github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/attachinterfaces/requests.go (about) 1 package attachinterfaces 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // List makes a request against the nova API to list the server's interfaces. 9 func List(client *gophercloud.ServiceClient, serverID string) pagination.Pager { 10 return pagination.NewPager(client, listInterfaceURL(client, serverID), func(r pagination.PageResult) pagination.Page { 11 return InterfacePage{pagination.SinglePageBase(r)} 12 }) 13 } 14 15 // Get requests details on a single interface attachment by the server and port IDs. 16 func Get(client *gophercloud.ServiceClient, serverID, portID string) (r GetResult) { 17 resp, err := client.Get(getInterfaceURL(client, serverID, portID), &r.Body, &gophercloud.RequestOpts{ 18 OkCodes: []int{200}, 19 }) 20 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 21 return 22 } 23 24 // CreateOptsBuilder allows extensions to add additional parameters to the 25 // Create request. 26 type CreateOptsBuilder interface { 27 ToAttachInterfacesCreateMap() (map[string]interface{}, error) 28 } 29 30 // CreateOpts specifies parameters of a new interface attachment. 31 type CreateOpts struct { 32 // PortID is the ID of the port for which you want to create an interface. 33 // The NetworkID and PortID parameters are mutually exclusive. 34 // If you do not specify the PortID parameter, the OpenStack Networking API 35 // v2.0 allocates a port and creates an interface for it on the network. 36 PortID string `json:"port_id,omitempty"` 37 38 // NetworkID is the ID of the network for which you want to create an interface. 39 // The NetworkID and PortID parameters are mutually exclusive. 40 // If you do not specify the NetworkID parameter, the OpenStack Networking 41 // API v2.0 uses the network information cache that is associated with the instance. 42 NetworkID string `json:"net_id,omitempty"` 43 44 // Slice of FixedIPs. If you request a specific FixedIP address without a 45 // NetworkID, the request returns a Bad Request (400) response code. 46 // Note: this uses the FixedIP struct, but only the IPAddress field can be used. 47 FixedIPs []FixedIP `json:"fixed_ips,omitempty"` 48 } 49 50 // ToAttachInterfacesCreateMap constructs a request body from CreateOpts. 51 func (opts CreateOpts) ToAttachInterfacesCreateMap() (map[string]interface{}, error) { 52 return gophercloud.BuildRequestBody(opts, "interfaceAttachment") 53 } 54 55 // Create requests the creation of a new interface attachment on the server. 56 func Create(client *gophercloud.ServiceClient, serverID string, opts CreateOptsBuilder) (r CreateResult) { 57 b, err := opts.ToAttachInterfacesCreateMap() 58 if err != nil { 59 r.Err = err 60 return 61 } 62 resp, err := client.Post(createInterfaceURL(client, serverID), b, &r.Body, &gophercloud.RequestOpts{ 63 OkCodes: []int{200}, 64 }) 65 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 66 return 67 } 68 69 // Delete makes a request against the nova API to detach a single interface from the server. 70 // It needs server and port IDs to make a such request. 71 func Delete(client *gophercloud.ServiceClient, serverID, portID string) (r DeleteResult) { 72 resp, err := client.Delete(deleteInterfaceURL(client, serverID, portID), nil) 73 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 74 return 75 }