|
| 1 | +#!/usr/bin/python |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +DOCUMENTATION = ''' |
| 15 | +--- |
| 16 | +module: as_instance_info |
| 17 | +short_description: Query Instances in an AS Group. |
| 18 | +extends_documentation_fragment: opentelekomcloud.cloud.otc |
| 19 | +version_added: "0.8.0" |
| 20 | +author: "Irina Pereiaslavskaia (@irina-pereiaslavskaia)" |
| 21 | +description: |
| 22 | + - This interface is used to query Instances in an AS Group \ |
| 23 | + based on search criteria. |
| 24 | +options: |
| 25 | + scaling_group: |
| 26 | + description: |
| 27 | + - Specifies the AS group name or ID. |
| 28 | + type: str |
| 29 | + required: true |
| 30 | + lifecycle_state: |
| 31 | + description: |
| 32 | + - Specifies the instance lifecycle status in the AS group. |
| 33 | + - If it set to INSERVICE, the instance is enabled. |
| 34 | + - If it set to PENDING, the instance is being added to the AS group. |
| 35 | + - If it set to REMOVING, the instance is being removed from the AS group. |
| 36 | + choices: [inservice, pending, removing] |
| 37 | + type: str |
| 38 | + health_status: |
| 39 | + description: |
| 40 | + - Specifies the instance health status. |
| 41 | + - If it set to INITIALIZING, the instance is initializing. |
| 42 | + - If it set to NORMAL, the instance is normal. |
| 43 | + - If it set to ERROR, the instance is abnormal. |
| 44 | + choices: [initializing, normal, error] |
| 45 | + type: str |
| 46 | + start_number: |
| 47 | + description: |
| 48 | + - Specifies the start line number. |
| 49 | + type: int |
| 50 | + default: 0 |
| 51 | + limit: |
| 52 | + description: |
| 53 | + - Specifies the number of query records. |
| 54 | + - The value range is 0 to 100. |
| 55 | + type: int |
| 56 | + default: 20 |
| 57 | +''' |
| 58 | + |
| 59 | +RETURN = ''' |
| 60 | +scaling_instances: |
| 61 | + description: |
| 62 | + - Query Instances in an AS Group based on search criteria. |
| 63 | + type: complex |
| 64 | + returned: success |
| 65 | + contains: |
| 66 | + total_number: |
| 67 | + description: |
| 68 | + - Specifies the total number of query records. |
| 69 | + type: int |
| 70 | + sample: 1 |
| 71 | + start_number: |
| 72 | + description: |
| 73 | + - Specifies the start line number. |
| 74 | + type: int |
| 75 | + sample: 10 |
| 76 | + limit: |
| 77 | + description: |
| 78 | + - Specifies the maximum number of resources to be queried. |
| 79 | + type: int |
| 80 | + sample: 10 |
| 81 | + scaling_group_instances: |
| 82 | + description: |
| 83 | + - Specifies details about the instances in the AS group. |
| 84 | + type: complex |
| 85 | + returned: success |
| 86 | + contains: |
| 87 | + instance_id: |
| 88 | + description: |
| 89 | + - Specifies the instance ID. |
| 90 | + type: str |
| 91 | + sample: "b25c1589-c96c-465b-9fef-d06540d1945c" |
| 92 | + instance_name: |
| 93 | + description: |
| 94 | + - Specifies the instance name. |
| 95 | + type: str |
| 96 | + sample: "discuz_3D210808" |
| 97 | + scaling_group_id: |
| 98 | + description: |
| 99 | + - Specifies the ID of the AS group to which the instance belongs. |
| 100 | + type: str |
| 101 | + sample: "e5d27f5c-dd76-4a61-b4bc-a67c5686719a" |
| 102 | + scaling_group_name: |
| 103 | + description: |
| 104 | + - Specifies the name of the AS group to which the instance belongs. |
| 105 | + - Supports fuzzy search. |
| 106 | + type: str |
| 107 | + sample: "test_group_name" |
| 108 | + lifecycle_state: |
| 109 | + description: |
| 110 | + - Specifies the instance lifecycle status in the AS group. |
| 111 | + - INSERVICE means that the instance is enabled. |
| 112 | + - PENDING means that the instance is being added to the AS group. |
| 113 | + - REMOVING means that the instance is being removed from the AS group. |
| 114 | + type: str |
| 115 | + sample: "INSERVICE" |
| 116 | + health_status: |
| 117 | + description: |
| 118 | + - Specifies the instance health status. |
| 119 | + - INITIALIZING means that the instance is being initialized. |
| 120 | + - NORMAL means that the instance is functional. |
| 121 | + - ERROR means that the instance is faulty. |
| 122 | + type: str |
| 123 | + sample: "NORMAL" |
| 124 | + scaling_configuration_name: |
| 125 | + description: |
| 126 | + - Specifies the AS configuration name. |
| 127 | + type: str |
| 128 | + sample: "test_config" |
| 129 | + scaling_configuration_id: |
| 130 | + description: |
| 131 | + - Specifies the AS configuration ID. |
| 132 | + - If the returned value is not empty, the instance is \ |
| 133 | + an ECS automatically created in a scaling action. |
| 134 | + - If the returned value is empty, the instance is \ |
| 135 | + an ECS manually added to the AS group. |
| 136 | + type: str |
| 137 | + sample: "ca3dcd84-d197-4c4f-af2a-cf8ba39696ac" |
| 138 | + create_time: |
| 139 | + description: |
| 140 | + - Specifies the time when the instance is added to the AS group. |
| 141 | + - The time format complies with UTC. |
| 142 | + type: str |
| 143 | + sample: "2021-02-23T06:47:33Z" |
| 144 | + protect_from_scaling_down: |
| 145 | + description: |
| 146 | + - Specifies the instance protection status. |
| 147 | + type: bool |
| 148 | + sample: "true" |
| 149 | +''' |
| 150 | + |
| 151 | +EXAMPLES = ''' |
| 152 | +# Get Instances in an AS Group |
| 153 | +- opentelekomcloud.cloud.as_instance_info: |
| 154 | + scaling_group: "89af599d-a8ab-4c29-a063-0b719ed77e8e" |
| 155 | + register: as_instances |
| 156 | +
|
| 157 | +# Get Instances in an AS Group |
| 158 | +- opentelekomcloud.cloud.as_instance_info: |
| 159 | + scaling_group: "test_group" |
| 160 | + start_number: 2 |
| 161 | + limit: 20 |
| 162 | + register: as_instances |
| 163 | +
|
| 164 | +# Get Instances in an AS Group |
| 165 | +- opentelekomcloud.cloud.as_instance_info: |
| 166 | + scaling_group: "89af599d-a8ab-4c29-a063-0b719ed77e8e" |
| 167 | + start_number: 2 |
| 168 | + limit: 20 |
| 169 | + register: as_instances |
| 170 | +''' |
| 171 | + |
| 172 | +from ansible_collections.opentelekomcloud.cloud.plugins.module_utils.otc import OTCModule |
| 173 | + |
| 174 | + |
| 175 | +class ASInstanceInfoModule(OTCModule): |
| 176 | + argument_spec = dict( |
| 177 | + scaling_group=dict(type='str', required=True), |
| 178 | + lifecycle_state=dict(type='str', required=False, |
| 179 | + choices=["inservice", "pending", "removing"]), |
| 180 | + health_status=dict(type='str', required=False, |
| 181 | + choices=["initializing", "normal", "error"]), |
| 182 | + start_number=dict(type='int', required=False, default=0), |
| 183 | + limit=dict(type='int', required=False, default=20) |
| 184 | + ) |
| 185 | + |
| 186 | + def run(self): |
| 187 | + as_group = self.params['scaling_group'] |
| 188 | + lifecycle_state = self.params['lifecycle_state'] |
| 189 | + health_status = self.params['health_status'] |
| 190 | + start_number = self.params['start_number'] |
| 191 | + limit = self.params['limit'] |
| 192 | + |
| 193 | + data = [] |
| 194 | + query = {} |
| 195 | + |
| 196 | + try: |
| 197 | + group = self.conn.auto_scaling.find_group( |
| 198 | + name_or_id=as_group, |
| 199 | + ignore_missing=False |
| 200 | + ) |
| 201 | + query['group'] = group.id |
| 202 | + |
| 203 | + except self.sdk.exceptions.ResourceNotFound: |
| 204 | + self.fail( |
| 205 | + changed=False, |
| 206 | + msg='Scaling group %s not found' % as_group |
| 207 | + ) |
| 208 | + |
| 209 | + if lifecycle_state: |
| 210 | + query['lifecycle_state'] = lifecycle_state.upper() |
| 211 | + |
| 212 | + if health_status: |
| 213 | + query['health_status'] = health_status |
| 214 | + |
| 215 | + if start_number >= 0: |
| 216 | + query['marker'] = start_number |
| 217 | + |
| 218 | + if 0 <= limit <= 100: |
| 219 | + query['limit'] = limit |
| 220 | + |
| 221 | + else: |
| 222 | + self.fail( |
| 223 | + changed=False, |
| 224 | + msg='Limit is out of range' |
| 225 | + ) |
| 226 | + |
| 227 | + for raw in self.conn.auto_scaling.instances(**query): |
| 228 | + dt = raw.to_dict() |
| 229 | + dt.pop('location') |
| 230 | + data.append(dt) |
| 231 | + |
| 232 | + self.exit( |
| 233 | + changed=False, |
| 234 | + scaling_instances=data |
| 235 | + ) |
| 236 | + |
| 237 | + |
| 238 | +def main(): |
| 239 | + module = ASInstanceInfoModule() |
| 240 | + module() |
| 241 | + |
| 242 | + |
| 243 | +if __name__ == '__main__': |
| 244 | + main() |
0 commit comments