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
88 changes: 88 additions & 0 deletions openstack/ecs/v1/nics/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package nics

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

// AssociateOptsBuilder allows extensions to
// add additional parameters to the associate request.
type AssociateOptsBuilder interface {
ToVIPAssociateMap() (map[string]interface{}, error)
}

// NicAssociateOpts is a struct for creation
type NicAssociateOpts struct {
SubnetID string `json:"subnet_id" required:"true"`
IPAddress string `json:"ip_address" required:"true"`
ReverseBinding bool `json:"reverse_binding" required:"true"`
DeviceOwner string `json:"device_owner,omitempty"`
}

// AssociateOpts specifies the required information
// to associate a Floating IP with an instance
type AssociateOpts struct {
// Nic is associated by VIP.
Nic NicAssociateOpts `json:"nic" required:"true"`
}

// ToVIPAssociateMap constructs a request body from AssociateOpts.
func (opts AssociateOpts) ToVIPAssociateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}

// AssociateVIP pairs an allocated VIP with a nic.
func AssociateVIP(client *golangsdk.ServiceClient, id string, ops AssociateOptsBuilder) (r AssociateResult) {
b, err := ops.ToVIPAssociateMap()
if err != nil {
r.Err = err
return
}

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

return
}

// DisassociateOptsBuilder allows extensions to
// add additional parameters to the Disassociate request.
type DisassociateOptsBuilder interface {
ToVIPDisassociateMap() (map[string]interface{}, error)
}

// NicDisassociateOpts is a struct for disassociate
type NicDisassociateOpts struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty struct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks your review

}

// DisassociateOpts specifies the required information
// to associate a VIP with a nic
type DisassociateOpts struct {
// Nic is associated by VIP.
Nic NicDisassociateOpts `json:"nic" required:"true"`
}

// ToVIPDisassociateMap constructs a request body from DisassociateOpts.
func (opts DisassociateOpts) ToVIPDisassociateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}

// DisassociateVIP pairs an allocated VIP with a nic.
func DisassociateVIP(client *golangsdk.ServiceClient, id string, ops DisassociateOptsBuilder) (r DisassociateResult) {
b, err := ops.ToVIPDisassociateMap()
if b != nil {
b["subnet_id"] = ""
b["ip_address"] = ""
b["reverse_binding"] = false
}
if err != nil {
r.Err = err
return
}

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

return
}
30 changes: 30 additions & 0 deletions openstack/ecs/v1/nics/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package nics

import "github.com/huaweicloud/golangsdk"

// Nic defines results
type Nic struct {
PortID string `json:"port_id"`
}

// AssociateResult is the response from a Associate operation.
type AssociateResult struct {
golangsdk.Result
}

// Extract from AssociateResult
func (r AssociateResult) Extract() (*Nic, error) {
s := &Nic{}
return s, r.ExtractInto(s)
}

// DisassociateResult is the response from a Disassociate operation.
type DisassociateResult struct {
golangsdk.Result
}

// Extract from DisassociateResult
func (r DisassociateResult) Extract() (*Nic, error) {
s := &Nic{}
return s, r.ExtractInto(s)
}
19 changes: 19 additions & 0 deletions openstack/ecs/v1/nics/urls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nics

import "github.com/huaweicloud/golangsdk"

// endpoint/cloudservers/
const (
rootPath = "cloudservers"
resourcePath = "nics"
)

// associateURL will build associate private ip from nic
func associateURL(c *golangsdk.ServiceClient, id string) string {
return c.ServiceURL(rootPath, resourcePath, id)
}

// disassociateURL will build disassociate private ip from nic
func disassociateURL(c *golangsdk.ServiceClient, id string) string {
return c.ServiceURL(rootPath, resourcePath, id)
}