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

56 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-10 19:46 +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_vpc_info 

33short_description: Gather facts on vpcs of Alibaba Cloud. 

34description: 

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

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

37options: 

38 vpc_name: 

39 description: 

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

41 aliases: ["name"] 

42 type: str 

43 vpc_ids: 

44 description: 

45 - A list of VPC IDs that exist in your account. 

46 aliases: ["ids"] 

47 type: list 

48 elements: str 

49 name_prefix: 

50 description: 

51 - Use a VPC name prefix to filter VPCs. 

52 type: str 

53 cidr_prefix: 

54 description: 

55 - Use a VPC cidr block prefix to filter VPCs. 

56 type: str 

57 filters: 

58 description: 

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

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

61 Filter keys can be same as request parameter name or be lower case and use underscore ("_") or dash ("-") to 

62 connect different words in one parameter. 'VpcId' will be appended to I(vpc_ids) automatically. 

63 type: dict 

64 tags: 

65 description: 

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

67 type: dict 

68author: 

69 - "Wang Yu (@zshongyi) 

70 - "He Guimin (@xiaozhu36)" 

71requirements: 

72 - "python >= 3.6" 

73 - "footmark >= 1.14.1" 

74extends_documentation_fragment: 

75 - apsarastack 

76''' 

77 

78EXAMPLES = ''' 

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

80 

81- name: Gather facts about all VPCs 

82 ali_vpc_info: 

83 

84- name: Gather facts about a particular VPC using VPC ID 

85 ali_vpc_info: 

86 vpc_ids: 

87 - vpc-aaabbb 

88 - vpc-123fwec 

89 

90- name: Gather facts about a particular VPC using VPC ID 

91 ali_vpc_info: 

92 name_prefix: "my-vpc" 

93 filters: 

94 is_default: False 

95 

96- name: Gather facts about any VPC with cidr_prefix 

97 ali_vpc_info: 

98 cidr_prefix: "172.16" 

99''' 

100 

101RETURN = ''' 

102ids: 

103 description: List all vpc's id after operating vpc. 

104 returned: when success 

105 type: list 

106 sample: ["vpc-2zegusms7jwd94lq7ix8o", "vpc-2ze5hrb3y5ksx5oa3a0xa"] 

107vpcs: 

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

109 returned: always 

110 type: complex 

111 contains: 

112 cidr_block: 

113 description: The CIDR of the VPC. 

114 returned: always 

115 type: str 

116 sample: 10.0.0.0/8 

117 creation_time: 

118 description: The time the VPC was created. 

119 returned: always 

120 type: str 

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

122 description: 

123 description: The VPC description. 

124 returned: always 

125 type: str 

126 sample: "my ansible vpc" 

127 id: 

128 description: alias of 'vpc_id'. 

129 returned: always 

130 type: str 

131 sample: vpc-c2e00da5 

132 is_default: 

133 description: indicates whether this is the default VPC. 

134 returned: always 

135 type: bool 

136 sample: false 

137 state: 

138 description: state of the VPC. 

139 returned: always 

140 type: str 

141 sample: available 

142 user_cidrs: 

143 description: The custom CIDR of the VPC. 

144 returned: always 

145 type: list 

146 sample: [] 

147 vpc_id: 

148 description: VPC resource id. 

149 returned: always 

150 type: str 

151 sample: vpc-c2e00da5 

152 vpc_name: 

153 description: Name of the VPC. 

154 returned: always 

155 type: str 

156 sample: my-vpc 

157 vrouter_id: 

158 description: The ID of virtual router which in the VPC. 

159 returned: always 

160 type: str 

161 sample: available 

162 vswitch_ids: 

163 description: List IDs of virtual switch which in the VPC. 

164 returned: always 

165 type: list 

166 sample: [vsw-123cce3, vsw-34cet4v] 

167''' 

168from ansible.module_utils.basic import AnsibleModule 

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

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

171 

172HAS_FOOTMARK = False 

173 

174try: 

175 from footmark.exception import VPCResponseError 

176 HAS_FOOTMARK = True 

177except ImportError: 

178 HAS_FOOTMARK = False 

179 

180 

181def main(): 

182 argument_spec = common_argument_spec() 

183 argument_spec.update(dict( 

184 vpc_ids=dict(type='list', elements='str', aliases=['ids']), 

185 vpc_name=dict(type='str', aliases=['name']), 

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

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

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

189 ) 

190 ) 

191 module = AnsibleModule(argument_spec=argument_spec) 

192 

193 if HAS_FOOTMARK is False: 

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

195 

196 filters = module.params['filters'] 

197 if not filters: 

198 filters = {} 

199 

200 vpc_ids = module.params['vpc_ids'] 

201 if not vpc_ids: 

202 vpc_ids = [] 

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

204 if key in ["VpcId", "vpc_id", "vpc-id"] and value not in vpc_ids: 

205 vpc_ids.append(value) 

206 

207 name = module.params['vpc_name'] 

208 name_prefix = module.params['name_prefix'] 

209 cidr_prefix = module.params['cidr_prefix'] 

210 

211 try: 

212 vpcs = [] 

213 ids = [] 

214 while True: 

215 if vpc_ids: 

216 filters['vpc_id'] = vpc_ids[0] 

217 vpc_ids.pop(0) 

218 for vpc in vpc_connect(module).describe_vpcs(**filters): 

219 if name and vpc.vpc_name != name: 

220 continue 

221 if name_prefix and not str(vpc.vpc_name).startswith(name_prefix): 

222 continue 

223 if cidr_prefix and not str(vpc.cidr_block).startswith(cidr_prefix): 

224 continue 

225 vpcs.append(vpc.read()) 

226 ids.append(vpc.id) 

227 if not vpc_ids: 

228 break 

229 

230 module.exit_json(changed=False, ids=ids, vpcs=vpcs) 

231 except Exception as e: 

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

233 

234 

235if __name__ == '__main__': 

236 main()