Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions openstack/networking/v1/vips/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package vips

import (
"github.com/huaweicloud/golangsdk"
)

// CreateOpsBuilder is an interface by which can build the request body of private ips
type CreateOpsBuilder interface {
ToPrivateIPMap() (map[string]interface{}, error)
}

// PrivateIPOpts is a struct for creation
type PrivateIPOpts struct {
SubnetID string `json:"subnet_id" required:"true"`
IPAddress string `json:"ip_address,omitempty"`
}

// CreateOps is a struct that contains all the parameters.
type CreateOps struct {
// The list of private ips.
PrivateIPs []PrivateIPOpts `json:"privateips" required:"true"`
}

// ToPrivateIPMap is used for type convert
func (opts CreateOps) ToPrivateIPMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}

// Create a private ip
func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder) (r CreateResult) {
b, err := ops.ToPrivateIPMap()
if err != nil {
r.Err = err
return
}

_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})

return
}

// Get is a method by which can get the detailed information of private ip
func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
return
}

// Delete is a method by which can be able to delete a private ip
func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
_, r.Err = client.Delete(deleteURL(client, id), nil)
return
}
54 changes: 54 additions & 0 deletions openstack/networking/v1/vips/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package vips

import (
"github.com/huaweicloud/golangsdk"
)

//CreateResult is a struct which represents the result of create private ip
type CreateResult struct {
golangsdk.Result
}

// PrivateIP is a struct that represents a private ip
type PrivateIP struct {
ID string `json:"id"`
Status string `json:"status"`
SubnetID string `json:"subnet_id"`
TenantID string `json:"tenant_id"`
DeviceOwner string `json:"device_owner"`
IPAddress string `json:"ip_address"`
}

// PrivateIPs is a struct that represents private ips
type PrivateIPs struct {
// The list of private ips.
PrivateIPs []PrivateIP `json:"privateips"`
}

// Extract from CreateResult
func (r CreateResult) Extract() (PrivateIPs, error) {
var s struct {
IPs PrivateIPs `json:"privateips"`
}
err := r.Result.ExtractInto(&s)
return s.IPs, err
}

// GetResult is a return struct of get method
type GetResult struct {
golangsdk.Result
}

// Extract from GetResult
func (r GetResult) Extract() (PrivateIP, error) {
var s struct {
IP PrivateIP `json:"privateip"`
}
err := r.Result.ExtractInto(&s)
return s.IP, err
}

// DeleteResult is a struct of delete result
type DeleteResult struct {
golangsdk.ErrResult
}
21 changes: 21 additions & 0 deletions openstack/networking/v1/vips/urls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package vips

import "github.com/huaweicloud/golangsdk"

// endpoint/{tenant_id}/privateips
const resourcePath = "privateips"

// createURL will build the rest query url of creation
func createURL(client *golangsdk.ServiceClient) string {
return client.ServiceURL(client.ProjectID, resourcePath)
}

// deleteURL will build the url of deletion
func deleteURL(client *golangsdk.ServiceClient, id string) string {
return client.ServiceURL(client.ProjectID, resourcePath, id)
}

// getURL will build the get url of get function
func getURL(client *golangsdk.ServiceClient, id string) string {
return client.ServiceURL(client.ProjectID, resourcePath, id)
}