/
ipaddressassociation.go
190 lines (151 loc) · 4.74 KB
/
ipaddressassociation.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
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
// Copyright 2017 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package api
import (
"errors"
"fmt"
"github.com/juju/go-oracle-cloud/response"
)
// IpAddressAssociation retrives details of the specified IP address association.
func (c *Client) IpAddressAssociationDetails(name string) (resp response.IpAddressAssociation, err error) {
if !c.isAuth() {
return resp, errNotAuth
}
if name == "" {
return resp, errors.New("go-oracle-cloud: Empty ip address association name")
}
url := fmt.Sprintf("%s%s", c.endpoints["ipaddressassociation"], name)
if err = c.request(paramsRequest{
url: url,
verb: "GET",
resp: &resp,
}); err != nil {
return resp, err
}
return resp, nil
}
// AllIpAddressAssociation Retrieves details of the specified IP address association.
func (c *Client) AllIpAddressAssociations(filter []Filter) (resp response.AllIpAddressAssociations, err error) {
if !c.isAuth() {
return resp, errNotAuth
}
url := fmt.Sprintf("%s/Compute-%s/%s/",
c.endpoints["ipaddressassociation"], c.identify, c.username)
if err = c.request(paramsRequest{
url: url,
verb: "GET",
resp: &resp,
filter: filter,
}); err != nil {
return resp, err
}
return resp, nil
}
// CreateIpAddressAssociation creates an IP address association
// to associate an IP address reservation, a public IP address,
// with a vNIC of an instance either while creating the instance
// or when an instance is already running.
func (c *Client) CreateIpAddressAssociation(
description string,
ipAddressReservation string,
vnic string,
name string,
tags []string,
) (resp response.IpAddressAssociation, err error) {
if !c.isAuth() {
return resp, errNotAuth
}
if name == "" {
return resp, errors.New("go-oracle-cloud: Empty ip address association name")
}
if ipAddressReservation == "" {
return resp, errors.New("go-oracle-cloud: Empty ip address association reservation")
}
if vnic == "" {
return resp, errors.New("go-oracle-cloud: Empty ip address association vnic")
}
// construct the body for the post request
params := response.IpAddressAssociation{
IpAddressReservation: ipAddressReservation,
Vnic: vnic,
Name: name,
Tags: tags,
Description: description,
}
url := c.endpoints["ipaddressassociation"] + "/"
if err = c.request(paramsRequest{
url: url,
verb: "POST",
resp: &resp,
body: ¶ms,
}); err != nil {
return resp, err
}
return resp, nil
}
// DeleteIpAddressAssociation deletes the specified IP address association.
// Ensure that the IP address association is not being used before deleting it.
func (c *Client) DeleteIpAddressAssociation(name string) (err error) {
if !c.isAuth() {
return errNotAuth
}
if name == "" {
return errors.New("go-oracle-cloud: Empty ip address association name")
}
url := fmt.Sprintf("%s%s", c.endpoints["ipaddressassociation"], name)
if err = c.request(paramsRequest{
url: url,
verb: "DELETE",
}); err != nil {
return err
}
return nil
}
// UpdateIpAddressAssociation updates the specified IP address association.
// You can update values for the following parameters
// of an IP address association: description, ipAddressReservation,
// vnic, and tags. If you associate an IP reservation with a vNIC while
// creating or updating the IP address association, then you can remove
// the association between the IP address reservation and vNIC by updating the IP address association.
// However, if you associate an IP reservation with an instance while creating the instance,
// then to remove the IP reservation, update the instance orchestration.
// Otherwise, whenever your instance orchestration is stopped and restarted,
// the IP reservation will again be associated with the vNIC.
func (c *Client) UpdateIpAddressAssociation(
currentName,
ipAddressReservation,
vnic,
newName string,
) (resp response.IpAddressAssociation, err error) {
if !c.isAuth() {
return resp, errNotAuth
}
if currentName == "" {
return resp, errors.New("go-oracle-cloud: Empty ip address association current name")
}
if ipAddressReservation == "" {
return resp, errors.New("go-oracle-cloud: Empty ip address association reservation")
}
if vnic == "" {
return resp, errors.New("go-oracle-cloud: Empty ip address association vnic")
}
if newName == "" {
newName = currentName
}
// construct the body for the post request
params := response.IpAddressAssociation{
IpAddressReservation: ipAddressReservation,
Vnic: vnic,
Name: newName,
}
url := fmt.Sprintf("%s%s", c.endpoints["ipaddressassociation"], currentName)
if err = c.request(paramsRequest{
url: url,
verb: "PUT",
resp: &resp,
body: ¶ms,
}); err != nil {
return resp, err
}
return resp, nil
}