This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
/
connection.go
96 lines (83 loc) · 3.17 KB
/
connection.go
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
// Copyright 2019, Pure Storage Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apiserver
import (
"fmt"
"github.com/PureStorage-OpenConnect/pure1-unplugged/pkg/common/resources"
"github.com/PureStorage-OpenConnect/pure1-unplugged/pkg/http"
"github.com/go-resty/resty"
log "github.com/sirupsen/logrus"
)
// Type guards: ensure this implements the interfaces
var _ resources.ArrayDiscovery = (*APIServer)(nil)
var _ resources.ArrayMetadata = (*APIServer)(nil)
// NewConnection establishes a connection with the API server
// at the given backend URL. Note that this returns a pointer to an API
// server, not to a specific interface, as it implements multiple interfaces.
func NewConnection(backendURL string) *APIServer {
return &APIServer{
serverEndpoint: backendURL,
}
}
// GetArrays is an implementation of the ArrayDiscovery interface
func (a *APIServer) GetArrays() ([]*resources.ArrayRegistrationInfo, error) {
log.WithField("endpoint", a.serverEndpoint).Trace("Starting API server device list GET")
uncastResponse, err := http.RestyGet(bulkDeviceResponse{}, resty.R(), fmt.Sprintf("%s/arrays", a.serverEndpoint))
if err != nil {
return nil, err
}
if response, ok := uncastResponse.(*bulkDeviceResponse); ok {
return response.Items, nil
}
return nil, fmt.Errorf("Error casting response to bulkDeviceResponse")
}
// Patch patches the given device with the given body
func (a *APIServer) Patch(arrayID string, body *resources.ArrayPatchInfo) error {
resp, err := resty.R().SetQueryParam("ids", arrayID).SetBody(*body).Patch(fmt.Sprintf("%s/arrays", a.serverEndpoint))
if err != nil {
return err
}
if resp.IsError() {
return fmt.Errorf("Error %s", resp.Status())
}
return nil
}
// GetTags fetches the tags for the given device from the API server
func (a *APIServer) GetTags(arrayID string) (map[string]string, error) {
log.WithField("endpoint", a.serverEndpoint).Trace("Starting API server device tags GET")
uncastResponse, err := http.RestyGet(bulkTagsResponse{}, resty.R().SetQueryParam("ids", arrayID), fmt.Sprintf("%s/arrays/tags", a.serverEndpoint))
if err != nil {
return nil, err
}
if response, ok := uncastResponse.(*bulkTagsResponse); ok {
if len(response.Items) == 0 {
return nil, fmt.Errorf("Response contained no arrays")
}
for _, array := range response.Items {
if array.ID != arrayID {
continue
}
parsedTags := map[string]string{}
for _, tag := range array.Tags {
if tag.Key == "" {
continue
}
parsedTags[tag.Key] = tag.Value
}
return parsedTags, nil
}
return nil, fmt.Errorf("Response did not contain an array with the matching ID")
}
return nil, fmt.Errorf("Error casting response to bulkTagsResponse")
}