Coverage for /Users/jingyu.wy/git/ansible-module-apsarastack/src/ansible_collections/alibaba/apsarastack/plugins/modules/ali_vswitch_info.py: 59%

64 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-11 11:21 +0800

1#!/usr/bin/python 

2# -*- coding: utf-8 -*- 

3 

4# Copyright (c) 2017-present Alibaba Group Holding Limited. He Guimin <heguimin36@163.com.com> 

5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 

6# 

7# This file is part of Ansible 

8# 

9# Ansible is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# Ansible is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with Ansible. If not, see http://www.gnu.org/licenses/. 

21 

22from __future__ import (absolute_import, division, print_function) 

23 

24__metaclass__ = type 

25 

26ANSIBLE_METADATA = {'metadata_version': '1.1', 

27 'status': ['preview'], 

28 'supported_by': 'community'} 

29 

30DOCUMENTATION = ''' 

31--- 

32module: ali_vswitch_info 

33short_description: Gather facts on vswitchs of Alibaba Cloud. 

34description: 

35 - This module fetches data from the Open API in Apsarastack. 

36 The module must be called from within the vswitch itself. 

37 

38options: 

39 vswitch_name: 

40 description: 

41 - (Deprecated) Name of one or more vswitch that exist in your account. New option `name_prefix` instead. 

42 aliases: ["name", 'subnet_name'] 

43 type: str 

44 vswitch_ids: 

45 description: 

46 - A list of vswitch IDs to gather facts for. 

47 aliases: ['subnet_ids', 'ids'] 

48 type: list 

49 elements: str 

50 cidr_block: 

51 description: 

52 - (Deprecated) The CIDR block representing the Vswitch e.g. 10.0.0.0/8. New option `cidr_prefix` instead. 

53 type: str 

54 name_prefix: 

55 description: 

56 - Use a VSwitch name prefix to filter vswitches. 

57 type: str 

58 cidr_prefix: 

59 description: 

60 - Use a VSwitch cidr block prefix to filter vswitches. 

61 type: str 

62 filters: 

63 description: 

64 - A dict of filters to apply. Each dict item consists of a filter key and a filter value. The filter keys can be 

65 all of request parameters. See U(https://www.alibabacloud.com/help/doc-detail/35748.htm) for parameter details. 

66 Filter keys can be same as request parameter name or be lower case and use underscores ("_") or dashes ("-") to 

67 connect different words in one parameter. 'VSwitchId' will be appended to I(vswitch_ids) automatically. 

68 type: dict 

69 tags: 

70 description: 

71 - A hash/dictionaries of vswitch tags. C({"key":"value"}) 

72 type: dict 

73author: 

74 - "He Guimin (@xiaozhu36)" 

75requirements: 

76 - "python >= 3.6" 

77 - "footmark >= 1.14.1" 

78extends_documentation_fragment: 

79 - apsarastack 

80''' 

81 

82EXAMPLES = ''' 

83# Note: These examples do not set authentication details, see the Alibaba Cloud Guide for details. 

84 

85- name: Gather facts about all VPC vswitches 

86 ali_vswitch_info: 

87 

88- name: Gather facts about a particular VPC subnet using ID 

89 ali_vswitch_info: 

90 vswitch_ids: [vsw-00112233] 

91 

92- name: Gather Gather facts about any VPC subnet within VPC with ID vpc-abcdef00 

93 ali_vswitch_info: 

94 filters: 

95 vpc-id: vpc-abcdef00 

96 

97- name: Gather facts about a set of VPC subnets, cidrA, cidrB and cidrC within a VPC 

98 ali_vswitch_info: 

99 cidr_prefix: "10.0." 

100 filters: 

101 vpc-id: vpc-abcdef00 

102''' 

103 

104RETURN = ''' 

105ids: 

106 description: List ids of being fetched vswtich. 

107 returned: when success 

108 type: list 

109 sample: ["vsw-2zegusms7jwd94lq7ix8o", "vsw-2ze5hrb3y5ksx5oa3a0xa"] 

110vswitches: 

111 description: Returns an array of complex objects as described below. 

112 returned: success 

113 type: complex 

114 contains: 

115 id: 

116 description: alias of vswitch_id 

117 returned: always 

118 type: str 

119 sample: vsw-b883b2c4 

120 cidr_block: 

121 description: The IPv4 CIDR of the VSwitch 

122 returned: always 

123 type: str 

124 sample: "10.0.0.0/16" 

125 availability_zone: 

126 description: Availability zone of the VSwitch 

127 returned: always 

128 type: str 

129 sample: cn-beijing-a 

130 state: 

131 description: state of the Subnet 

132 returned: always 

133 type: str 

134 sample: available 

135 is_default: 

136 description: indicates whether this is the default VSwitch 

137 returned: always 

138 type: bool 

139 sample: false 

140 tags: 

141 description: tags attached to the Subnet, includes name 

142 returned: always 

143 type: dict 

144 sample: {"Name": "My Subnet", "env": "staging"} 

145 vpc_id: 

146 description: the id of the VPC where this VSwitch exists 

147 returned: always 

148 type: str 

149 sample: vpc-67236184 

150 available_ip_address_count: 

151 description: number of available IPv4 addresses 

152 returned: always 

153 type: str 

154 sample: 250 

155 vswitch_id: 

156 description: VSwitch resource id 

157 returned: always 

158 type: str 

159 sample: vsw-b883b2c4 

160 subnet_id: 

161 description: alias of vswitch_id 

162 returned: always 

163 type: str 

164 sample: vsw-b883b2c4 

165 vswitch_name: 

166 description: VSwitch resource name 

167 returned: always 

168 type: str 

169 sample: my-vsw 

170 creation_time: 

171 description: The time the VSwitch was created. 

172 returned: always 

173 type: str 

174 sample: '2018-06-24T15:14:45Z' 

175''' 

176 

177from ansible.module_utils.basic import AnsibleModule 

178try: 

179 from ansible_collections.alibaba.apsarastack.plugins.module_utils.apsarastack_common import common_argument_spec 

180 from ansible_collections.alibaba.apsarastack.plugins.module_utils.apsarastack_connections import vpc_connect 

181except ImportError: 

182 from ..module_utils.apsarastack_common import common_argument_spec 

183 from ..module_utils.apsarastack_connections import vpc_connect 

184HAS_FOOTMARK = False 

185 

186try: 

187 from footmark.exception import VPCResponseError 

188 HAS_FOOTMARK = True 

189except ImportError: 

190 HAS_FOOTMARK = False 

191 

192 

193def main(): 

194 argument_spec = common_argument_spec() 

195 argument_spec.update(dict( 

196 vswitch_name=dict(type='str', aliases=['name', 'subnet_name']), 

197 cidr_block=dict(type='str'), 

198 name_prefix=dict(type='str'), 

199 cidr_prefix=dict(type='str'), 

200 vswitch_ids=dict(type='list', elements='str', aliases=['ids', 'subnet_ids']), 

201 filters=dict(type='dict'), 

202 tags=dict(type='dict') 

203 ) 

204 ) 

205 

206 module = AnsibleModule(argument_spec=argument_spec) 

207 

208 if HAS_FOOTMARK is False: 

209 module.fail_json(msg="Package 'footmark' required for this module.") 

210 

211 filters = module.params['filters'] 

212 if not filters: 

213 filters = {} 

214 

215 vswitch_ids = module.params['vswitch_ids'] 

216 if not vswitch_ids: 

217 vswitch_ids = [] 

218 for key, value in list(filters.items()): 

219 if key in ["VSwitchId", "vswitch_id", "vswitch-id"] and value not in vswitch_ids: 

220 vswitch_ids.append(value) 

221 

222 name = module.params['vswitch_name'] 

223 cidr_block = module.params['cidr_block'] 

224 name_prefix = module.params['name_prefix'] 

225 cidr_prefix = module.params['cidr_prefix'] 

226 tags = module.params['tags'] 

227 

228 try: 

229 vswitches = [] 

230 ids = [] 

231 while True: 

232 if vswitch_ids: 

233 filters['vswitch_id'] = vswitch_ids[0] 

234 vswitch_ids.pop(0) 

235 for vsw in vpc_connect(module).describe_vswitches(**filters): 

236 if name and vsw.vswitch_name != name: 

237 continue 

238 if cidr_block and vsw.cidr_block != cidr_block: 

239 continue 

240 if name_prefix and not str(vsw.vswitch_name).startswith(name_prefix): 

241 continue 

242 if cidr_prefix and not str(vsw.cidr_block).startswith(cidr_prefix): 

243 continue 

244 vswitches.append(vsw.read()) 

245 ids.append(vsw.id) 

246 if not vswitch_ids: 

247 break 

248 

249 module.exit_json(changed=False, ids=ids, vswitches=vswitches) 

250 except Exception as e: 

251 module.fail_json(msg=str("Unable to get vswitches, error:{0}".format(e))) 

252 

253 

254if __name__ == '__main__': 

255 main()