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

70 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-14 11:58 +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_dns_domain 

33short_description: Configure Alibaba Cloud DNS (DNS) 

34description: 

35 - Create, Delete Apsarastack cloud DNS(DNS). 

36 It supports updating DNS remark and change domain group. 

37options: 

38 domain_name: 

39 description: 

40 - The name to give your DNS. 

41 required: True 

42 aliases: ['name'] 

43 type: str 

44 group_name: 

45 description: 

46 - Specify name of group, when change domain group. 

47 type: str 

48 lang: 

49 description: 

50 - The language which you choose 

51 type: str 

52 resource_group_id: 

53 description: 

54 - When add domain, You can specify the resource group id. 

55 type: str 

56 remark: 

57 description: 

58 - Specify this parameter as a comment for dns. 

59 type: str 

60 state: 

61 description: 

62 - Whether or not to create, delete DNS. 

63 choices: ['present', 'absent'] 

64 default: 'present' 

65 type: str 

66 user_client_ip: 

67 description: 

68 - User client IP. 

69 type: str 

70requirements: 

71 - "python >= 3.6" 

72 - "footmark >= 1.15.0" 

73extends_documentation_fragment: 

74 - apsarastack 

75author: 

76 - "He Guimin (@xiaozhu36)" 

77""" 

78 

79EXAMPLES = """ 

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

81- name: Create a new domain 

82 ali_dns_domain: 

83 state: 'present' 

84 domain_name: '{{ domain_name }}' 

85 remark: '{{ remark }}' 

86 

87- name: Changed. Changed. Modify remark. 

88 ali_dns_domain: 

89 domain_name: '{{ domain_name }}' 

90 remark: 'new--{{ remark }}' 

91 

92- name: Changed. change domain group. 

93 ali_dns_domain: 

94 domain_name: '{{ domain_name }}' 

95 group_name: '{{ group_name }}' 

96 

97- name: Changed. Deleting dns 

98 ali_dns_domain: 

99 domain_name: '{{ domain_name }}' 

100 state: absent 

101""" 

102 

103RETURN = ''' 

104dns: 

105 description: info about the DNS that was created or deleted. 

106 returned: always 

107 type: complex 

108 contains: 

109 ali_domain: 

110 description: Whether it is the domain name of Alibaba Cloud. 

111 returned: always 

112 type: bool 

113 sample: false 

114 dns_servers: 

115 description: The DNS list of the domain name in the resolution system. 

116 returned: always 

117 type: dict 

118 sample:  

119 dns_servers: 

120 dns_server:  

121 - xx1.alidns.com 

122 - xx2.alidns.com 

123 

124 domain_name: 

125 description: The name of domain. 

126 returned: always 

127 type: str 

128 sample: ansiblexxx.abc 

129 name: 

130 description: alias of 'domain_name'. 

131 returned: always 

132 type: str 

133 sample: ansiblexxx.abc 

134 id: 

135 description: alias of 'domain_id'. 

136 returned: always 

137 type: str 

138 sample: dns-c2e00da5 

139 puny_code: 

140 description: Chinese domain name punycode code, English domain name returned empty. 

141 type: bool 

142 sample: ansiblexxx.abc 

143 record_count: 

144 description: The number of parsing records contained in the domain name. 

145 returned: always 

146 type: int 

147 sample: 0 

148 remark: 

149 description: A comment for dns. 

150 returned: always 

151 type: str 

152 sample: ansible_test_dns_domain 

153 starmark: 

154 description: Whether to query the domain name star. 

155 returned: always 

156 type: bool 

157 sample: false 

158 domain_id: 

159 description: DNS resource id. 

160 returned: always 

161 type: str 

162 sample: dns-c2e00da5 

163 version_code: 

164 description: Cloud resolution version Code. 

165 returned: always 

166 type: str 

167 sample: mianfei 

168 version_name: 

169 description: Cloud resolution product name. 

170 returned: always 

171 type: str 

172 sample: Alibaba Cloud DNS 

173''' 

174 

175from ansible.module_utils.basic import AnsibleModule 

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

177from ansible_collections.alibaba.apsarastack.plugins.module_utils.apsarastack_connections import dns_connect 

178 

179HAS_FOOTMARK = False 

180 

181try: 

182 from footmark.exception import DNSResponseError 

183 HAS_FOOTMARK = True 

184except ImportError: 

185 HAS_FOOTMARK = False 

186 

187def dns_exists(module, dns_conn, domain_name): 

188 """Returns None or a vpc object depending on the existence of a VPC. When supplied 

189 with a CIDR and Name, it will check them to determine if it is a match 

190 otherwise it will assume the VPC does not exist and thus return None. 

191 """ 

192 matching_dns = [] 

193 try: 

194 for v in dns_conn.describe_domains(): 

195 if v.domain_name == domain_name: 

196 matching_dns.append(v) 

197 except Exception as e: 

198 module.fail_json(msg="Failed to describe DNSs: {0}".format(e)) 

199 

200 if matching_dns: 

201 return matching_dns[0] 

202 

203 

204def main(): 

205 argument_spec = common_argument_spec() 

206 argument_spec.update(dict( 

207 domain_name=dict(type='str', aliases=['name'], required=True), 

208 group_name=dict(type='str'), 

209 lang=dict(type='str'), 

210 resource_group_id=dict(type='str'), 

211 remark=dict(type='str'), 

212 state=dict(default='present', choices=['present', 'absent']), 

213 user_client_ip=dict(type='str') 

214 )) 

215 

216 module = AnsibleModule(argument_spec=argument_spec) 

217 

218 if HAS_FOOTMARK is False: 

219 module.fail_json(msg='footmark required for the module ali_dns_domain.') 

220 

221 dns_conn = dns_connect(module) 

222 

223 # Get values of variable 

224 state = module.params['state'] 

225 domain_name = module.params['domain_name'] 

226 remark = module.params['remark'] 

227 group_name = module.params['group_name'] 

228 changed = False 

229 

230 # Check if VPC exists 

231 dns = dns_exists(module, dns_conn, domain_name) 

232 

233 if state == 'absent': 

234 if not dns: 

235 module.exit_json(changed=changed, dns={}) 

236 try: 

237 module.exit_json(changed=dns.delete(), dns={}) 

238 except DNSResponseError as ex: 

239 module.fail_json(msg='Unable to delete dns {0}, error: {1}'.format(dns.id, ex)) 

240 

241 if not dns: 

242 params = module.params 

243 try: 

244 dns = dns_conn.add_domain(**params) 

245 if dns: 

246 changed = True 

247 except DNSResponseError as e: 

248 module.fail_json(msg='Unable to create dns, error: {0}'.format(e)) 

249 

250 if domain_name and group_name: 

251 try: 

252 res = dns.change_domain_group(group_name=group_name, domain_name=domain_name) 

253 if res: 

254 changed = True 

255 except DNSResponseError as e: 

256 module.fail_json(msg='Unable to change domain group, error: {0}'.format(e)) 

257 

258 if remark: 

259 try: 

260 res = dns.modify_remark(remark=remark) 

261 if res: 

262 changed = True 

263 except DNSResponseError as e: 

264 module.fail_json(msg='Unable to modify dns remark, error: {0}'.format(e)) 

265 module.exit_json(changed=changed, dns=dns.get().read()) 

266 

267 

268if __name__ == '__main__': 

269 main()