-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathvpc.go
More file actions
205 lines (180 loc) · 6.33 KB
/
vpc.go
File metadata and controls
205 lines (180 loc) · 6.33 KB
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
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* 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 simple
import (
"context"
"net/http"
"time"
"github.com/NVIDIA/infra-controller-rest/sdk/standard"
)
// Vpc represents a simplified VPC
type Vpc struct {
ID string `json:"id"`
Name string `json:"name"`
Description *string `json:"description"`
NetworkVirtualizationType string `json:"networkVirtualizationType"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
}
// VpcCreateRequest is a request to create a VPC
type VpcCreateRequest struct {
Name string `json:"name"`
Description *string `json:"description"`
NetworkVirtualizationType string `json:"networkVirtualizationType"`
}
// VpcUpdateRequest is a request to update a VPC
type VpcUpdateRequest struct {
Name *string `json:"name"`
Description *string `json:"description"`
}
// VpcFilter encapsulates VPC filter parameters
type VpcFilter struct {
SiteID *string
}
// VpcManager manages VPC operations
type VpcManager struct {
client *Client
}
// NewVpcManager creates a new VpcManager
func NewVpcManager(client *Client) VpcManager {
return VpcManager{client: client}
}
func toStandardVpcCreateRequest(request VpcCreateRequest, siteID string) standard.VpcCreateRequest {
apiReq := *standard.NewVpcCreateRequest(request.Name, siteID)
if request.Description != nil {
apiReq.SetDescription(*request.Description)
}
if request.NetworkVirtualizationType != "" {
apiReq.SetNetworkVirtualizationType(request.NetworkVirtualizationType)
}
return apiReq
}
func toStandardVpcUpdateRequest(request VpcUpdateRequest) standard.VpcUpdateRequest {
apiReq := standard.VpcUpdateRequest{}
if request.Name != nil {
apiReq.SetName(*request.Name)
}
if request.Description != nil {
apiReq.SetDescription(*request.Description)
}
return apiReq
}
func vpcFromStandard(api standard.VPC) Vpc {
v := Vpc{}
if api.Id != nil {
v.ID = *api.Id
}
if api.Name != nil {
v.Name = *api.Name
}
v.Description = api.Description.Get()
if api.NetworkVirtualizationType.IsSet() {
v.NetworkVirtualizationType = *api.NetworkVirtualizationType.Get()
}
if api.Created != nil {
v.Created = *api.Created
}
if api.Updated != nil {
v.Updated = *api.Updated
}
return v
}
// CreateVpc creates a new VPC
func (vm VpcManager) CreateVpc(ctx context.Context, request VpcCreateRequest) (*Vpc, *ApiError) {
ctx = WithLogger(ctx, vm.client.Logger)
ctx = context.WithValue(ctx, standard.ContextAccessToken, vm.client.Config.Token)
apiReq := toStandardVpcCreateRequest(request, vm.client.apiMetadata.SiteID)
apiVpc, resp, err := vm.client.apiClient.VPCAPI.CreateVpc(ctx, vm.client.apiMetadata.Organization).
VpcCreateRequest(apiReq).Execute()
apiErr := HandleResponseError(resp, err)
if apiErr != nil {
return nil, apiErr
}
v := vpcFromStandard(*apiVpc)
return &v, nil
}
// GetVpcs returns all VPCs
func (vm VpcManager) GetVpcs(ctx context.Context, vpcFilter *VpcFilter, paginationFilter *PaginationFilter) ([]Vpc, *standard.PaginationResponse, *ApiError) {
ctx = WithLogger(ctx, vm.client.Logger)
ctx = context.WithValue(ctx, standard.ContextAccessToken, vm.client.Config.Token)
gvr := vm.client.apiClient.VPCAPI.GetAllVpc(ctx, vm.client.apiMetadata.Organization)
if vpcFilter != nil && vpcFilter.SiteID != nil {
gvr = gvr.SiteId(*vpcFilter.SiteID)
}
if paginationFilter != nil {
if paginationFilter.PageNumber != nil {
gvr = gvr.PageNumber(int32(*paginationFilter.PageNumber))
}
if paginationFilter.PageSize != nil {
gvr = gvr.PageSize(int32(*paginationFilter.PageSize))
}
if paginationFilter.OrderBy != nil {
gvr = gvr.OrderBy(*paginationFilter.OrderBy)
}
}
apiVpcs, resp, err := gvr.Execute()
apiErr := HandleResponseError(resp, err)
if apiErr != nil {
return nil, nil, apiErr
}
vpcs := make([]Vpc, 0, len(apiVpcs))
for _, apiVpc := range apiVpcs {
vpcs = append(vpcs, vpcFromStandard(apiVpc))
}
paginationResponse, perr := standard.GetPaginationResponse(ctx, resp)
if perr != nil {
return nil, nil, &ApiError{
Code: http.StatusInternalServerError,
Message: "failed to extract pagination: " + perr.Error(),
Data: map[string]interface{}{"parseError": perr.Error()},
}
}
return vpcs, paginationResponse, nil
}
// GetVpc returns a VPC by ID
func (vm VpcManager) GetVpc(ctx context.Context, id string) (*Vpc, *ApiError) {
ctx = WithLogger(ctx, vm.client.Logger)
ctx = context.WithValue(ctx, standard.ContextAccessToken, vm.client.Config.Token)
apiVpc, resp, err := vm.client.apiClient.VPCAPI.GetVpc(ctx, vm.client.apiMetadata.Organization, id).Execute()
apiErr := HandleResponseError(resp, err)
if apiErr != nil {
return nil, apiErr
}
v := vpcFromStandard(*apiVpc)
return &v, nil
}
// UpdateVpc updates a VPC
func (vm VpcManager) UpdateVpc(ctx context.Context, id string, request VpcUpdateRequest) (*Vpc, *ApiError) {
ctx = WithLogger(ctx, vm.client.Logger)
ctx = context.WithValue(ctx, standard.ContextAccessToken, vm.client.Config.Token)
apiReq := toStandardVpcUpdateRequest(request)
apiVpc, resp, err := vm.client.apiClient.VPCAPI.UpdateVpc(ctx, vm.client.apiMetadata.Organization, id).
VpcUpdateRequest(apiReq).Execute()
apiErr := HandleResponseError(resp, err)
if apiErr != nil {
return nil, apiErr
}
v := vpcFromStandard(*apiVpc)
return &v, nil
}
// DeleteVpc deletes a VPC
func (vm VpcManager) DeleteVpc(ctx context.Context, id string) *ApiError {
ctx = WithLogger(ctx, vm.client.Logger)
ctx = context.WithValue(ctx, standard.ContextAccessToken, vm.client.Config.Token)
resp, err := vm.client.apiClient.VPCAPI.DeleteVpc(ctx, vm.client.apiMetadata.Organization, id).Execute()
return HandleResponseError(resp, err)
}