@@ -173,6 +173,121 @@ func (c *Client) UpdateVPCNetwork(id string, nc NetworkConfig) (*NetworkResult,
173173 return result , nil
174174}
175175
176+ // =============================================================================
177+ // VPC Subnets - /v2/vpc/networks/{network_id}/subnets
178+ // =============================================================================
179+
180+ // GetVPCSubnet gets a subnet with ID using VPC API path
181+ func (c * Client ) GetVPCSubnet (networkID , subnetID string ) (* Subnet , error ) {
182+ resp , err := c .SendGetRequest (fmt .Sprintf ("/v2/vpc/networks/%s/subnets/%s" , networkID , subnetID ))
183+ if err != nil {
184+ return nil , decodeError (err )
185+ }
186+
187+ subnet := Subnet {}
188+ err = json .NewDecoder (bytes .NewReader (resp )).Decode (& subnet )
189+ return & subnet , err
190+ }
191+
192+ // ListVPCSubnets lists all subnets for a private network using VPC API path
193+ func (c * Client ) ListVPCSubnets (networkID string ) ([]Subnet , error ) {
194+ resp , err := c .SendGetRequest (fmt .Sprintf ("/v2/vpc/networks/%s/subnets" , networkID ))
195+ if err != nil {
196+ return nil , decodeError (err )
197+ }
198+
199+ subnets := make ([]Subnet , 0 )
200+ if err := json .NewDecoder (bytes .NewReader (resp )).Decode (& subnets ); err != nil {
201+ return nil , err
202+ }
203+
204+ return subnets , nil
205+ }
206+
207+ // CreateVPCSubnet creates a new subnet for a private network using VPC API path
208+ func (c * Client ) CreateVPCSubnet (networkID string , subnet SubnetConfig ) (* Subnet , error ) {
209+ body , err := c .SendPostRequest (fmt .Sprintf ("/v2/vpc/networks/%s/subnets" , networkID ), subnet )
210+ if err != nil {
211+ return nil , decodeError (err )
212+ }
213+
214+ var result = & Subnet {}
215+ if err := json .NewDecoder (bytes .NewReader (body )).Decode (result ); err != nil {
216+ return nil , err
217+ }
218+
219+ return result , nil
220+ }
221+
222+ // FindVPCSubnet finds a subnet by either part of the ID or part of the name using VPC API path
223+ func (c * Client ) FindVPCSubnet (search , networkID string ) (* Subnet , error ) {
224+ subnets , err := c .ListVPCSubnets (networkID )
225+ if err != nil {
226+ return nil , decodeError (err )
227+ }
228+
229+ exactMatch := false
230+ partialMatchesCount := 0
231+ result := Subnet {}
232+
233+ for _ , value := range subnets {
234+ if value .Name == search || value .ID == search {
235+ exactMatch = true
236+ result = value
237+ } else if strings .Contains (value .Name , search ) || strings .Contains (value .ID , search ) {
238+ if ! exactMatch {
239+ result = value
240+ partialMatchesCount ++
241+ }
242+ }
243+ }
244+
245+ if exactMatch || partialMatchesCount == 1 {
246+ return & result , nil
247+ } else if partialMatchesCount > 1 {
248+ err := fmt .Errorf ("unable to find %s because there were multiple matches" , search )
249+ return nil , MultipleMatchesError .wrap (err )
250+ } else {
251+ err := fmt .Errorf ("unable to find %s, zero matches" , search )
252+ return nil , ZeroMatchesError .wrap (err )
253+ }
254+ }
255+
256+ // AttachVPCSubnetToInstance attaches a subnet to an instance using VPC API path
257+ func (c * Client ) AttachVPCSubnetToInstance (networkID , subnetID string , route * CreateRoute ) (* Route , error ) {
258+ resp , err := c .SendPostRequest (fmt .Sprintf ("/v2/vpc/networks/%s/subnets/%s/routes" , networkID , subnetID ), route )
259+ if err != nil {
260+ return nil , decodeError (err )
261+ }
262+
263+ var result = & Route {}
264+ if err := json .NewDecoder (bytes .NewReader (resp )).Decode (result ); err != nil {
265+ return nil , err
266+ }
267+
268+ return result , nil
269+ }
270+
271+ // DetachVPCSubnetFromInstance detaches a subnet from an instance using VPC API path
272+ func (c * Client ) DetachVPCSubnetFromInstance (networkID , subnetID string ) (* SimpleResponse , error ) {
273+ resp , err := c .SendDeleteRequest (fmt .Sprintf ("/v2/vpc/networks/%s/subnets/%s/routes" , networkID , subnetID ))
274+ if err != nil {
275+ return nil , decodeError (err )
276+ }
277+
278+ return c .DecodeSimpleResponse (resp )
279+ }
280+
281+ // DeleteVPCSubnet deletes a subnet using VPC API path
282+ func (c * Client ) DeleteVPCSubnet (networkID , subnetID string ) (* SimpleResponse , error ) {
283+ resp , err := c .SendDeleteRequest (fmt .Sprintf ("/v2/vpc/networks/%s/subnets/%s" , networkID , subnetID ))
284+ if err != nil {
285+ return nil , decodeError (err )
286+ }
287+
288+ return c .DecodeSimpleResponse (resp )
289+ }
290+
176291// =============================================================================
177292// VPC Firewalls - /v2/vpc/firewalls
178293// =============================================================================
0 commit comments