aboutsummaryrefslogtreecommitdiff
path: root/content/blog/golang/golang-netapp-api.md
blob: 0d0d27d4d1851239127b8dce01692d040d1c94ba (plain)
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
---
title: Writing golang REST client to query netapp API
description: I needed a tool to migrate all network interfaces quickly
date: 2021-06-23
tags:
- golang
---

## Introduction

Yesterday I had to prepare a maintenance operation on one of my employers' netapp FAS8200 clusters. This cluster has two nodes, each with two uplinks using LACP. All these uplinks had to be moved to a new pair of switches : goodbye juniper EX4200, hello arista 7050SX3!

To make sure the operation is without disruption we needed to migrate all lifs (virtual network interfaces that serve NFS traffic) before disconnecting a node. The cluster has 137 such lifs, so manually migrating those was out of question.

Fortunately our netapp clusters are kept up to date by yours truly so we have access to the modern netapp api's. If that had not been the case I would have had to script the lif migrations by issuing cli commands and parsing the outputs... It could have worked but it would not have been clean and subject to breaking when lif names get too long or garbled, error handling would have suffered, etc.

A few years ago I would have done it in perl, but perl jobs being in decline I try to force myself to write more golang whenever I have the opportunity. I also could have leveraged existing libraries but just wanted to perform this exercise by myself. I had a few hours before me and was confident it would be more than enough to write and test it. Also I wanted something to blog about ;-)

## A little golang lib

First let's create a simple golang project :
```sh
mkdir netapp-lif-migrate
cd !$
go mod init !$
mkdir lib
```

Since we are going to need a few tools, I split those in a lib folder.

### lib/client.go

This is some boilerplate to have a clean REST client :
```golang
package lib

import (
	"fmt"
	"net/http"
	"time"
)

type Client struct {
	baseURL    string
	httpClient *http.Client
}

func NewClient(login string, password string, url string) *Client {
	return &Client{
		baseURL: fmt.Sprintf("https://%s:%s@%s/api", login, password, url),
		httpClient: &http.Client{
				Timeout: time.Minute,
		},
	}
}
```

### lib/model.go

Here we write some data structures to parse api responses for the few objects we are going to need :
```golang
package lib

type Lif struct {
	UUID     string   `json:"uuid"`
	Name     string   `json:"name"`
	Location Location `json:"location"`
}

type Location struct {
	Node     Node `json:"node"`
	HomeNode Node `json:"home_node"`
}

type Node struct {
	Name string `json:"name"`
}
```

### lib/lif.go

Here comes the two api calls we need : one to fetch the lif's status and one to migrate a lif. Error handling is kept simple :
```golang
package lib

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
)

type LifsResponse struct {
	Records []Lif `json:"records"`
}

func (c *Client) GetAllLifs() ([]Lif, error) {
	request := fmt.Sprintf("%s/network/ip/interfaces?fields=name,location.home_node.name,location.node.name", c.baseURL)
	req, err := http.NewRequest("GET", request, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Add("Accept", "application/json")
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode == http.StatusOK {
		var data LifsResponse
		if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
				return nil, err
		}
		return data.Records, nil
	} else {
		return nil, err
	}
}

func (c *Client) MigrateLif(uuid string, targetNode string) error {
	request := fmt.Sprintf("%s/network/ip/interfaces/%s", c.baseURL, uuid)
	fmt.Println(request)
	req, err := http.NewRequest("PATCH", request,
		strings.NewReader(fmt.Sprintf(`{ "location": { "node": { "name": "%s" } } }`, targetNode))
	)
	if err != nil {
		return err
	}
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Accept", "application/json")
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode == http.StatusOK {
		return nil
	} else {
		return fmt.Errorf("http error: %d", resp.StatusCode)
	}
}
```

### lib/password.go

The last piece to our lib is some code to prompt for a password on the terminal :
```golang
package lib

import (
	"fmt"
	"os"

	"golang.org/x/term"
)

func AskPassword(message string) (string, error) {
	// Print message
	fmt.Printf("%s: ", message)

	// Set the terminal for password input
	oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
	if err != nil {
		return "", err
	}
	defer term.Restore(int(os.Stdin.Fd()), oldState)

	// Ask for the netapp's password
	password, err := term.ReadPassword(int(os.Stdin.Fd()))
	if err != nil {
		return "", err
	}
	return string(password), nil
}
```

## Getting all lifs' status

With our simple lib, we can isolate the operations in individual scripts. I did not want to handle arguments, I wanted just simple scripts to execute with as little room for errors as possible : one script for each task. All this netapp migration is just a small part of a four hours maintenance by night, I needed the simplicity to avoid any potential mistake or in case I was not the one operating.

Here is the script to query the netapp's status :
```golang
package main

import (
	"fmt"
	"log"
	"netapp-lif-migrate/lib"
	"os"
)

func main() {
	password, err := lib.AskPassword("Password for admin@mut-CT-02")
	if err != nil {
		log.Println("Error when asking for password: %+v", err)
		os.Exit(1)
	}
	client := lib.NewClient("admin", password, "mut-ct-02.example.com")
	lifs, err := client.GetAllLifs()
	if err != nil {
		log.Println("Error getting all lifs: %+v", err)
		os.Exit(2)
	}
	for i := 0; i < len(lifs); i++ {
		isHome := "yes"
		if lifs[i].Location.HomeNode.Name != lifs[i].Location.Node.Name {
				isHome = "no"
		}
		fmt.Printf("home: %s, \tname: %s, \thome_node: %s, \tcurrent_node: %s\n",
				isHome,
				lifs[i].Name,
				lifs[i].Location.HomeNode.Name,
				lifs[i].Location.Node.Name,
		)
	}
}
```

## Migrate all lifs to a node

I had two copies of the following script, one for each node which differ only with the targetNode content :
```golang
package main

import (
	"fmt"
	"log"
	"netapp-lif-migrate/lib"
	"os"
)

targetNode := "mut-CT-02-01"

func main() {
	password, err := lib.AskPassword("Password for admin@mut-CT-02")
	if err != nil {
		log.Println("Error when asking for password: %+v", err)
		os.Exit(1)
	}
	client := lib.NewClient("admin", password, "mut-ct-02.example.com")
	lifs, err := client.GetAllLifs()
	if err != nil {
		log.Println("Error getting all lifs: %+v", err)
		os.Exit(2)
	}
	for i := 0; i < len(lifs); i++ {
		if lifs[i].Location.Node.Name != targetNode {
				err = client.MigrateLif(lifs[i].UUID, targetNode)
				if err == nil {
					fmt.Printf("Migrated %s\n", lifs[i].Name)
				} else {
					fmt.Printf("Failed to migrate %s\n", lifs[i].Name)
				}
		}
	}
}
```

## Send everyone home

The final script is one that takes all lifs that are not on their home port and send them to it :
```golang
package main

import (
	"fmt"
	"log"
	"netapp-lif-migrate/lib"
	"os"
)

func main() {
	password, err := lib.AskPassword("Password for admin@mut-CT-02")
	if err != nil {
		log.Println("Error when asking for password: %+v", err)
		os.Exit(1)
	}
	client := lib.NewClient("admin", password, "mut-ct-02.example.com")
	lifs, err := client.GetAllLifs()
	if err != nil {
		log.Println("Error getting all lifs: %+v", err)
		os.Exit(2)
	}
	for i := 0; i < len(lifs); i++ {
		if lifs[i].Location.Node.Name != lifs[i].Location.HomeNode.Name {
				err = client.MigrateLif(lifs[i].UUID, lifs[i].Location.HomeNode.Name)
				if err == nil {
					fmt.Printf("Migrated %s\n", lifs[i].Name)
				} else {
					fmt.Printf("Failed to migrate %s\n", lifs[i].Name)
				}
		}
	}
}
```

## Conclusion

This was a great golang exercise and the maintenance operation was a success.

I tried to parallelize the migration calls but I kept getting rate limited by the netapp so I settled on a simple sequential approach.