Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

#!/usr/bin/python 

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

 

# Copyright: (c) 2019, Kevin Breit (@kbreit) <kevin.breit@kevinbreit.net> 

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

 

from __future__ import absolute_import, division, print_function 

__metaclass__ = type 

 

ANSIBLE_METADATA = { 

'metadata_version': '1.1', 

'status': ['preview'], 

'supported_by': 'community' 

} 

 

DOCUMENTATION = r''' 

--- 

module: meraki_mx_uplink 

short_description: Manage uplinks on Meraki MX appliances 

version_added: "2.9" 

description: 

- Configure and query information about uplinks on Meraki MX appliances. 

notes: 

- Some of the options are likely only used for developers within Meraki. 

options: 

state: 

description: 

- Specifies whether object should be queried, created/modified, or removed. 

choices: [absent, present, query] 

default: query 

type: str 

org_name: 

description: 

- Name of organization associated to a network. 

type: str 

org_id: 

description: 

- ID of organization associated to a network. 

type: str 

net_name: 

description: 

- Name of network which VLAN is in or should be in. 

aliases: [network] 

type: str 

net_id: 

description: 

- ID of network which VLAN is in or should be in. 

type: str 

wan1: 

description: 

- Configuration of WAN1 uplink 

type: dict 

suboptions: 

bandwidth_limits: 

description: 

- Structure for configuring bandwidth limits 

type: dict 

suboptions: 

limit_up: 

description: 

- Maximum upload speed for interface 

type: int 

limit_down: 

description: 

- Maximum download speed for interface 

type: int 

wan2: 

description: 

- Configuration of WAN2 uplink 

type: dict 

suboptions: 

bandwidth_limits: 

description: 

- Structure for configuring bandwidth limits 

type: dict 

suboptions: 

limit_up: 

description: 

- Maximum upload speed for interface 

type: int 

limit_down: 

description: 

- Maximum download speed for interface 

type: int 

cellular: 

description: 

- Configuration of cellular uplink 

type: dict 

suboptions: 

bandwidth_limits: 

description: 

- Structure for configuring bandwidth limits 

type: dict 

suboptions: 

limit_up: 

description: 

- Maximum upload speed for interface 

type: int 

limit_down: 

description: 

- Maximum download speed for interface 

type: int 

author: 

- Kevin Breit (@kbreit) 

extends_documentation_fragment: meraki 

''' 

 

EXAMPLES = r''' 

- name: Set MX uplink settings 

meraki_mx_uplink: 

auth_key: '{{auth_key}}' 

state: present 

org_name: '{{test_org_name}}' 

net_name: '{{test_net_name}} - Uplink' 

wan1: 

bandwidth_limits: 

limit_down: 1000000 

limit_up: 1000 

cellular: 

bandwidth_limits: 

limit_down: 0 

limit_up: 0 

delegate_to: localhost 

 

- name: Query MX uplink settings 

meraki_mx_uplink: 

auth_key: '{{auth_key}}' 

state: query 

org_name: '{{test_org_name}}' 

net_name: '{{test_net_name}} - Uplink' 

delegate_to: localhost 

 

''' 

 

RETURN = r''' 

 

data: 

description: Information about the organization which was created or modified 

returned: success 

type: complex 

contains: 

wan1: 

description: WAN1 interface 

returned: success 

type: complex 

contains: 

bandwidth_limits: 

description: Structure for uplink bandwidth limits 

returned: success 

type: complex 

contains: 

limit_up: 

description: Upload bandwidth limit 

returned: success 

type: int 

limit_down: 

description: Download bandwidth limit 

returned: success 

type: int 

wan2: 

description: WAN2 interface 

returned: success 

type: complex 

contains: 

bandwidth_limits: 

description: Structure for uplink bandwidth limits 

returned: success 

type: complex 

contains: 

limit_up: 

description: Upload bandwidth limit 

returned: success 

type: int 

limit_down: 

description: Download bandwidth limit 

returned: success 

type: int 

cellular: 

description: cellular interface 

returned: success 

type: complex 

contains: 

bandwidth_limits: 

description: Structure for uplink bandwidth limits 

returned: success 

type: complex 

contains: 

limit_up: 

description: Upload bandwidth limit 

returned: success 

type: int 

limit_down: 

description: Download bandwidth limit 

returned: success 

type: int 

''' 

 

from ansible.module_utils.basic import AnsibleModule, json 

from ansible.module_utils.common.dict_transformations import recursive_diff 

from ansible.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec 

 

INT_NAMES = ('wan1', 'wan2', 'cellular') 

 

 

def clean_custom_format(data): 

206 ↛ 211line 206 didn't jump to line 211, because the loop on line 206 didn't complete for interface in data: 

207 ↛ 209line 207 didn't jump to line 209, because the condition on line 207 was never false if data[interface]['bandwidth_limits']['limit_up'] is None: 

data[interface]['bandwidth_limits']['limit_up'] = 0 

209 ↛ 206,   209 ↛ 2102 missed branches: 1) line 209 didn't jump to line 206, because the condition on line 209 was never false, 2) line 209 didn't jump to line 210, because the condition on line 209 was never true if data[interface]['bandwidth_limits']['limit_down'] is None: 

data[interface]['bandwidth_limits']['limit_down'] = 0 

return data 

 

 

def meraki_struct_to_custom_format(data): 

new_struct = {} 

216 ↛ 223line 216 didn't jump to line 223, because the loop on line 216 didn't complete for interface in INT_NAMES: 

217 ↛ 216line 217 didn't jump to line 216, because the condition on line 217 was never false if interface in data['bandwidthLimits']: 

new_struct[interface] = {'bandwidth_limits': {'limit_up': data['bandwidthLimits'][interface]['limitUp'], 

'limit_down': data['bandwidthLimits'][interface]['limitDown'], 

} 

} 

# return snake_dict_to_camel_dict(new_struct) 

return new_struct 

 

 

def main(): 

# define the available arguments/parameters that a user can pass to 

# the module 

 

bandwidth_arg_spec = dict(limit_up=dict(type='int'), 

limit_down=dict(type='int'), 

) 

 

interface_arg_spec = dict(bandwidth_limits=dict(type='dict', default=None, options=bandwidth_arg_spec), 

) 

 

argument_spec = meraki_argument_spec() 

argument_spec.update(state=dict(type='str', choices=['absent', 'present', 'query'], default='query'), 

net_name=dict(type='str', aliases=['network']), 

net_id=dict(type='str'), 

wan1=dict(type='dict', default=None, options=interface_arg_spec), 

wan2=dict(type='dict', default=None, options=interface_arg_spec), 

cellular=dict(type='dict', default=None, options=interface_arg_spec), 

) 

 

# the AnsibleModule object will be our abstraction working with Ansible 

# this includes instantiation, a couple of common attr would be the 

# args/params passed to the execution, as well as if the module 

# supports check mode 

module = AnsibleModule(argument_spec=argument_spec, 

supports_check_mode=True, 

) 

meraki = MerakiModule(module, function='mx_uplink') 

 

meraki.params['follow_redirects'] = 'all' 

 

query_urls = {'mx_uplink': '/networks/{net_id}/uplinkSettings'} 

update_bw_url = {'mx_uplink': '/networks/{net_id}/uplinkSettings'} 

 

meraki.url_catalog['get_all'].update(query_urls) 

meraki.url_catalog['update_bw'] = update_bw_url 

 

payload = dict() 

 

org_id = meraki.params['org_id'] 

if org_id is None: 

org_id = meraki.get_org_id(meraki.params['org_name']) 

net_id = meraki.params['net_id'] 

if net_id is None: 

nets = meraki.get_nets(org_id=org_id) 

net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) 

 

273 ↛ 278line 273 didn't jump to line 278, because the condition on line 273 was never false if meraki.params['state'] == 'query': 

path = meraki.construct_path('get_all', net_id=net_id) 

response = meraki.request(path, method='GET') 

data = clean_custom_format(meraki_struct_to_custom_format(response)) 

meraki.result['data'] = data 

278 ↛ 279,   278 ↛ 3202 missed branches: 1) line 278 didn't jump to line 279, because the condition on line 278 was never true, 2) line 278 didn't jump to line 320, because the condition on line 278 was never false elif meraki.params['state'] == 'present': 

path = meraki.construct_path('get_all', net_id=net_id) 

original = meraki.request(path, method='GET') 

payload = {'bandwidthLimits': {}} 

282 ↛ 293line 282 didn't jump to line 293, because the loop on line 282 didn't complete for interface in INT_NAMES: 

283 ↛ 282line 283 didn't jump to line 282, because the condition on line 283 was never false if meraki.params[interface] is not None: 

284 ↛ 282,   284 ↛ 2852 missed branches: 1) line 284 didn't jump to line 282, because the condition on line 284 was never false, 2) line 284 didn't jump to line 285, because the condition on line 284 was never true if meraki.params[interface]['bandwidth_limits'] is not None: 

payload['bandwidthLimits'][interface] = None 

payload['bandwidthLimits'][interface] = {'limitUp': meraki.params[interface]['bandwidth_limits']['limit_up'], 

'limitDown': meraki.params[interface]['bandwidth_limits']['limit_down'], 

} 

289 ↛ 291line 289 didn't jump to line 291, because the condition on line 289 was never false if payload['bandwidthLimits'][interface]['limitUp'] == 0: 

payload['bandwidthLimits'][interface]['limitUp'] = None 

291 ↛ 282line 291 didn't jump to line 282, because the condition on line 291 was never false if payload['bandwidthLimits'][interface]['limitDown'] == 0: 

payload['bandwidthLimits'][interface]['limitDown'] = None 

293 ↛ 316line 293 didn't jump to line 316, because the condition on line 293 was never false if meraki.is_update_required(original, payload): 

294 ↛ 304line 294 didn't jump to line 304, because the condition on line 294 was never false if meraki.module.check_mode is True: 

diff = recursive_diff(clean_custom_format(meraki_struct_to_custom_format(original)), 

clean_custom_format(meraki_struct_to_custom_format(payload))) 

original.update(payload) 

meraki.result['data'] = clean_custom_format(meraki_struct_to_custom_format(original)) 

meraki.result['diff'] = {'before': diff[0], 

'after': diff[1], 

} 

meraki.result['changed'] = True 

meraki.exit_json(**meraki.result) 

path = meraki.construct_path('update_bw', net_id=net_id) 

response = meraki.request(path, method='PUT', payload=json.dumps(payload)) 

306 ↛ 320line 306 didn't jump to line 320, because the condition on line 306 was never false if meraki.status == 200: 

formatted_original = clean_custom_format(meraki_struct_to_custom_format(original)) 

formatted_response = clean_custom_format(meraki_struct_to_custom_format(response)) 

diff = recursive_diff(formatted_original, formatted_response) 

meraki.result['diff'] = {'before': diff[0], 

'after': diff[1], 

} 

meraki.result['data'] = formatted_response 

meraki.result['changed'] = True 

else: 

meraki.result['data'] = clean_custom_format(meraki_struct_to_custom_format(original)) 

 

# in the event of a successful module execution, you will want to 

# simple AnsibleModule.exit_json(), passing the key/value results 

meraki.exit_json(**meraki.result) 

 

 

323 ↛ exit,   323 ↛ 3242 missed branches: 1) line 323 didn't exit the module, because the condition on line 323 was never false, 2) line 323 didn't jump to line 324, because the condition on line 323 was never trueif __name__ == '__main__': 

main()