diff --git a/openstack/swr/v2/namespaces/requests.go b/openstack/swr/v2/namespaces/requests.go new file mode 100644 index 00000000..da9d0ca2 --- /dev/null +++ b/openstack/swr/v2/namespaces/requests.go @@ -0,0 +1,55 @@ +package namespaces + +import ( + "github.com/huaweicloud/golangsdk" +) + +var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ + MoreHeaders: map[string]string{"Content-Type": "application/json"}, +} + +// CreateOptsBuilder allows extensions to add additional parameters to the +// Create request. +type CreateOptsBuilder interface { + ToNamespaceCreateMap() (map[string]interface{}, error) +} + +// CreateOpts contains all the values needed to create a new network +type CreateOpts struct { + Namespace string `json:"namespace" required:"true"` +} + +// ToNamespaceCreateMap builds a create request body from CreateOpts. +func (opts CreateOpts) ToNamespaceCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "") +} + +// Create accepts a CreateOpts struct and uses the values to create a new namespace. +func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { + b, err := opts.ToNamespaceCreateMap() + if err != nil { + r.Err = err + return + } + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}} + _, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt) + return +} + +// Get retrieves a particular network based on its unique ID. +func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { + _, r.Err = c.Get(resourceURL(c, id), &r.Body, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, + }) + return +} + +// Delete will permanently delete a particular network based on its unique ID. +func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { + _, r.Err = c.Delete(resourceURL(c, id), &golangsdk.RequestOpts{ + OkCodes: []int{204}, + MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, + }) + return +} diff --git a/openstack/swr/v2/namespaces/results.go b/openstack/swr/v2/namespaces/results.go new file mode 100644 index 00000000..bd2c85fa --- /dev/null +++ b/openstack/swr/v2/namespaces/results.go @@ -0,0 +1,42 @@ +package namespaces + +import ( + "github.com/huaweicloud/golangsdk" +) + +type Namespace struct { + // Name of the Namespace + Name string `json:"name"` + // Creator Name of the Namespace + CreatorName string `json:"creator_name"` + // Auth permission of the Namespace + Auth int `json:"auth"` +} + +type commonResult struct { + golangsdk.Result +} + +// Extract is a function that accepts a result and extracts a namespace. +func (r commonResult) Extract() (*Namespace, error) { + var s Namespace + err := r.ExtractInto(&s) + return &s, err +} + +// CreateResult represents the result of a create operation. +type CreateResult struct { + golangsdk.ErrResult +} + +// GetResult represents the result of a get operation. Call its Extract +// method to interpret it as a Network. +type GetResult struct { + commonResult +} + +// DeleteResult represents the result of a delete operation. Call its ExtractErr +// method to determine if the request succeeded or failed. +type DeleteResult struct { + golangsdk.ErrResult +} diff --git a/openstack/swr/v2/namespaces/urls.go b/openstack/swr/v2/namespaces/urls.go new file mode 100644 index 00000000..c430f24b --- /dev/null +++ b/openstack/swr/v2/namespaces/urls.go @@ -0,0 +1,11 @@ +package namespaces + +import "github.com/huaweicloud/golangsdk" + +func rootURL(client *golangsdk.ServiceClient) string { + return client.ServiceURL("manage", "namespaces") +} + +func resourceURL(client *golangsdk.ServiceClient, name string) string { + return client.ServiceURL("manage", "namespaces", name) +}