From a31bf9d2cd367b48c56867759add3fc22d67090a Mon Sep 17 00:00:00 2001 From: edisonxiang Date: Tue, 20 Mar 2018 15:43:03 +0800 Subject: [PATCH] add vips for networking --- openstack/networking/v1/vips/requests.go | 54 ++++++++++++++++++++++++ openstack/networking/v1/vips/results.go | 54 ++++++++++++++++++++++++ openstack/networking/v1/vips/urls.go | 21 +++++++++ 3 files changed, 129 insertions(+) create mode 100644 openstack/networking/v1/vips/requests.go create mode 100644 openstack/networking/v1/vips/results.go create mode 100644 openstack/networking/v1/vips/urls.go diff --git a/openstack/networking/v1/vips/requests.go b/openstack/networking/v1/vips/requests.go new file mode 100644 index 000000000..0c4503cfc --- /dev/null +++ b/openstack/networking/v1/vips/requests.go @@ -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 +} diff --git a/openstack/networking/v1/vips/results.go b/openstack/networking/v1/vips/results.go new file mode 100644 index 000000000..bad22a48d --- /dev/null +++ b/openstack/networking/v1/vips/results.go @@ -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 +} diff --git a/openstack/networking/v1/vips/urls.go b/openstack/networking/v1/vips/urls.go new file mode 100644 index 000000000..1a8ad521d --- /dev/null +++ b/openstack/networking/v1/vips/urls.go @@ -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) +}