File: requests.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 0.0~git20180917.45f1c769-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,768 kB
  • sloc: sh: 98; makefile: 14
file content (102 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package extradhcpopts

import (
	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
)

// CreateOptsExt adds extra DHCP options to the base ports.CreateOpts.
type CreateOptsExt struct {
	// CreateOptsBuilder is the interface options structs have to satisfy in order
	// to be used in the main Create operation in this package.
	ports.CreateOptsBuilder

	// ExtraDHCPOpts field is a set of DHCP options for a single port.
	ExtraDHCPOpts []CreateExtraDHCPOpt `json:"extra_dhcp_opts,omitempty"`
}

// CreateExtraDHCPOpt represents the options required to create an extra DHCP
// option on a port.
type CreateExtraDHCPOpt struct {
	// OptName is the name of a DHCP option.
	OptName string `json:"opt_name" required:"true"`

	// OptValue is the value of the DHCP option.
	OptValue string `json:"opt_value" required:"true"`

	// IPVersion is the IP protocol version of a DHCP option.
	IPVersion gophercloud.IPVersion `json:"ip_version,omitempty"`
}

// ToPortCreateMap casts a CreateOptsExt struct to a map.
func (opts CreateOptsExt) ToPortCreateMap() (map[string]interface{}, error) {
	base, err := opts.CreateOptsBuilder.ToPortCreateMap()
	if err != nil {
		return nil, err
	}

	port := base["port"].(map[string]interface{})

	// Convert opts.ExtraDHCPOpts to a slice of maps.
	if opts.ExtraDHCPOpts != nil {
		extraDHCPOpts := make([]map[string]interface{}, len(opts.ExtraDHCPOpts))
		for i, opt := range opts.ExtraDHCPOpts {
			b, err := gophercloud.BuildRequestBody(opt, "")
			if err != nil {
				return nil, err
			}
			extraDHCPOpts[i] = b
		}
		port["extra_dhcp_opts"] = extraDHCPOpts
	}

	return base, nil
}

// UpdateOptsExt adds extra DHCP options to the base ports.UpdateOpts.
type UpdateOptsExt struct {
	// UpdateOptsBuilder is the interface options structs have to satisfy in order
	// to be used in the main Update operation in this package.
	ports.UpdateOptsBuilder

	// ExtraDHCPOpts field is a set of DHCP options for a single port.
	ExtraDHCPOpts []UpdateExtraDHCPOpt `json:"extra_dhcp_opts,omitempty"`
}

// UpdateExtraDHCPOpt represents the options required to update an extra DHCP
// option on a port.
type UpdateExtraDHCPOpt struct {
	// OptName is the name of a DHCP option.
	OptName string `json:"opt_name" required:"true"`

	// OptValue is the value of the DHCP option.
	OptValue *string `json:"opt_value"`

	// IPVersion is the IP protocol version of a DHCP option.
	IPVersion gophercloud.IPVersion `json:"ip_version,omitempty"`
}

// ToPortUpdateMap casts an UpdateOpts struct to a map.
func (opts UpdateOptsExt) ToPortUpdateMap() (map[string]interface{}, error) {
	base, err := opts.UpdateOptsBuilder.ToPortUpdateMap()
	if err != nil {
		return nil, err
	}

	port := base["port"].(map[string]interface{})

	// Convert opts.ExtraDHCPOpts to a slice of maps.
	if opts.ExtraDHCPOpts != nil {
		extraDHCPOpts := make([]map[string]interface{}, len(opts.ExtraDHCPOpts))
		for i, opt := range opts.ExtraDHCPOpts {
			b, err := gophercloud.BuildRequestBody(opt, "")
			if err != nil {
				return nil, err
			}
			extraDHCPOpts[i] = b
		}
		port["extra_dhcp_opts"] = extraDHCPOpts
	}

	return base, nil
}