File: network.go

package info (click to toggle)
golang-github-vultr-govultr 2.17.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 604 kB
  • sloc: makefile: 2
file content (144 lines) | stat: -rw-r--r-- 5,139 bytes parent folder | download
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
package govultr

import (
	"context"
	"fmt"
	"net/http"

	"github.com/google/go-querystring/query"
)

const netPath = "/v2/private-networks"

// NetworkService is the interface to interact with the network endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/private-Networks
// Deprecated: NetworkService should no longer be used. Instead, use VPCService.
type NetworkService interface {
	// Deprecated: NetworkService Create should no longer be used. Instead, use VPCService Create.
	Create(ctx context.Context, createReq *NetworkReq) (*Network, error)
	// Deprecated: NetworkService Get should no longer be used. Instead, use VPCService Get.
	Get(ctx context.Context, networkID string) (*Network, error)
	// Deprecated: NetworkService Update should no longer be used. Instead, use VPCService Update.
	Update(ctx context.Context, networkID string, description string) error
	// Deprecated: NetworkService Delete should no longer be used. Instead, use VPCService Delete.
	Delete(ctx context.Context, networkID string) error
	// Deprecated: NetworkService List should no longer be used. Instead, use VPCService List.
	List(ctx context.Context, options *ListOptions) ([]Network, *Meta, error)
}

// NetworkServiceHandler handles interaction with the network methods for the Vultr API
// Deprecated: NetworkServiceHandler should no longer be used. Instead, use VPCServiceHandler.
type NetworkServiceHandler struct {
	client *Client
}

// Network represents a Vultr private network
// Deprecated: Network should no longer be used. Instead, use VPC.
type Network struct {
	NetworkID    string `json:"id"`
	Region       string `json:"region"`
	Description  string `json:"description"`
	V4Subnet     string `json:"v4_subnet"`
	V4SubnetMask int    `json:"v4_subnet_mask"`
	DateCreated  string `json:"date_created"`
}

// NetworkReq represents parameters to create or update Network resource
// Deprecated: NetworkReq should no longer be used. Instead, use VPCReq.
type NetworkReq struct {
	Region       string `json:"region"`
	Description  string `json:"description"`
	V4Subnet     string `json:"v4_subnet"`
	V4SubnetMask int    `json:"v4_subnet_mask"`
}

type networksBase struct {
	Networks []Network `json:"networks"`
	Meta     *Meta     `json:"meta"`
}

type networkBase struct {
	Network *Network `json:"network"`
}

// Create a new private network. A private network can only be used at the location for which it was created.
// Deprecated: NetworkServiceHandler Create should no longer be used. Instead, use VPCServiceHandler Create.
func (n *NetworkServiceHandler) Create(ctx context.Context, createReq *NetworkReq) (*Network, error) {
	req, err := n.client.NewRequest(ctx, http.MethodPost, netPath, createReq)
	if err != nil {
		return nil, err
	}

	network := new(networkBase)
	if err = n.client.DoWithContext(ctx, req, network); err != nil {
		return nil, err
	}

	return network.Network, nil
}

// Get gets the private networks of the requested ID
// Deprecated: NetworkServiceHandler Get should no longer be used.  Instead use VPCServiceHandler Create.
func (n *NetworkServiceHandler) Get(ctx context.Context, networkID string) (*Network, error) {
	uri := fmt.Sprintf("%s/%s", netPath, networkID)
	req, err := n.client.NewRequest(ctx, http.MethodGet, uri, nil)
	if err != nil {
		return nil, err
	}

	network := new(networkBase)
	if err = n.client.DoWithContext(ctx, req, network); err != nil {
		return nil, err
	}

	return network.Network, nil
}

// Update updates a private network
// Deprecated: NetworkServiceHandler Update should no longer be used. Instead, use VPCServiceHandler Update.
func (n *NetworkServiceHandler) Update(ctx context.Context, networkID string, description string) error {
	uri := fmt.Sprintf("%s/%s", netPath, networkID)

	netReq := RequestBody{"description": description}
	req, err := n.client.NewRequest(ctx, http.MethodPut, uri, netReq)
	if err != nil {
		return err
	}

	return n.client.DoWithContext(ctx, req, nil)
}

// Delete a private network. Before deleting, a network must be disabled from all instances
// Deprecated: NetworkServiceHandler Delete should no longer be used. Instead, use VPCServiceHandler Delete.
func (n *NetworkServiceHandler) Delete(ctx context.Context, networkID string) error {
	uri := fmt.Sprintf("%s/%s", netPath, networkID)
	req, err := n.client.NewRequest(ctx, http.MethodDelete, uri, nil)
	if err != nil {
		return err
	}

	return n.client.DoWithContext(ctx, req, nil)
}

// List lists all private networks on the current account
// Deprecated: NetworkServiceHandler List should no longer be used. Instead, use VPCServiceHandler List.
func (n *NetworkServiceHandler) List(ctx context.Context, options *ListOptions) ([]Network, *Meta, error) {
	req, err := n.client.NewRequest(ctx, http.MethodGet, netPath, nil)
	if err != nil {
		return nil, nil, err
	}

	newValues, err := query.Values(options)
	if err != nil {
		return nil, nil, err
	}

	req.URL.RawQuery = newValues.Encode()

	networks := new(networksBase)
	if err = n.client.DoWithContext(ctx, req, networks); err != nil {
		return nil, nil, err
	}

	return networks.Networks, networks.Meta, nil
}