Skip to content

Commit

Permalink
reserved IPs: allow setting project_id on create. (#1233)
Browse files Browse the repository at this point in the history
* Bump godo to v1.84.1

* reserved IPs: allow setting project_id on create.
  • Loading branch information
andrewsomething authored Sep 20, 2022
1 parent e0e8895 commit f5e6747
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 49 deletions.
2 changes: 2 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ const (
// ArgOutboundRules is a list of outbound rules for the firewall.
ArgOutboundRules = "outbound-rules"

// ArgProjectID is the ID of a project.
ArgProjectID = "project-id"
// ArgProjectName is the name of a project.
ArgProjectName = "name"
// ArgProjectDescription is the description of a project.
Expand Down
5 changes: 3 additions & 2 deletions commands/displayers/reserved_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func (rip *ReservedIP) JSON(out io.Writer) error {

func (rip *ReservedIP) Cols() []string {
return []string{
"IP", "Region", "DropletID", "DropletName",
"IP", "Region", "DropletID", "DropletName", "ProjectID",
}
}

func (rip *ReservedIP) ColMap() map[string]string {
return map[string]string{
"IP": "IP", "Region": "Region", "DropletID": "Droplet ID", "DropletName": "Droplet Name",
"IP": "IP", "Region": "Region", "DropletID": "Droplet ID", "DropletName": "Droplet Name", "ProjectID": "Project ID",
}
}

Expand All @@ -55,6 +55,7 @@ func (rip *ReservedIP) KV() []map[string]interface{} {
o := map[string]interface{}{
"IP": f.IP, "Region": f.Region.Slug,
"DropletID": dropletID, "DropletName": dropletName,
"ProjectID": f.ProjectID,
}

out = append(out, o)
Expand Down
11 changes: 10 additions & 1 deletion commands/reserved_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ A reserved IP address must be either assigned to a Droplet or reserved to a regi
AddStringFlag(cmdReservedIPCreate, doctl.ArgRegionSlug, "", "",
fmt.Sprintf("Region where to create the reserved IP address. (mutually exclusive with `--%s`)",
doctl.ArgDropletID))
AddStringFlag(cmdReservedIPCreate, doctl.ArgProjectID, "", "",
fmt.Sprintf("The ID of the project the reserved IP address will be assigned to. When excluded, it will be assigned to the default project. When using the `--%s` flag, it will be assigned to the project containing the Droplet.",
doctl.ArgDropletID))
AddIntFlag(cmdReservedIPCreate, doctl.ArgDropletID, "", 0,
fmt.Sprintf("The ID of the Droplet to assign the reserved IP to (mutually exclusive with `--%s`).",
doctl.ArgRegionSlug))
Expand All @@ -67,18 +70,24 @@ func RunReservedIPCreate(c *CmdConfig) error {
// ignore errors since we don't know which one is valid
region, _ := c.Doit.GetString(c.NS, doctl.ArgRegionSlug)
dropletID, _ := c.Doit.GetInt(c.NS, doctl.ArgDropletID)
projectID, _ := c.Doit.GetString(c.NS, doctl.ArgProjectID)

if region == "" && dropletID == 0 {
return doctl.NewMissingArgsErr("Region and Droplet ID can't both be blank.")
}

if region != "" && dropletID != 0 {
return fmt.Errorf("Specify region or Droplet ID when creating a reserved IP address.")
return fmt.Errorf("Only one of `--%s` or `--%s` may be specified when creating a reserved IP address.", doctl.ArgRegionSlug, doctl.ArgDropletID)
}

if projectID != "" && dropletID != 0 {
return fmt.Errorf("Only one of `--%s` or `--%s` may be specified when creating a reserved IP address.", doctl.ArgProjectID, doctl.ArgDropletID)
}

req := &godo.ReservedIPCreateRequest{
Region: region,
DropletID: dropletID,
ProjectID: projectID,
}

ip, err := ris.Create(req)
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/containerd/continuity v0.3.0 // indirect
github.com/creack/pty v1.1.11
github.com/digitalocean/godo v1.83.0
github.com/digitalocean/godo v1.84.1
github.com/docker/cli v20.10.17+incompatible
github.com/docker/docker v17.12.0-ce-rc1.0.20200531234253-77e06fda0c94+incompatible // indirect
github.com/docker/docker-credential-helpers v0.6.4 // indirect
Expand Down Expand Up @@ -44,7 +44,10 @@ require (
sigs.k8s.io/yaml v1.2.0
)

require github.com/apache/openwhisk-client-go v0.0.0-20211007130743-38709899040b
require (
github.com/apache/openwhisk-client-go v0.0.0-20211007130743-38709899040b
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
github.com/Microsoft/go-winio v0.5.2 // indirect
Expand Down Expand Up @@ -86,7 +89,6 @@ require (
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8l
github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/digitalocean/godo v1.83.0 h1:K9CveJyECNLwrQnGZG+ovgapr7l5OuvQ6xZSKKW9Nz0=
github.com/digitalocean/godo v1.83.0/go.mod h1:BPCqvwbjbGqxuUnIKB4EvS/AX7IDnNmt5fwvIkWo+ew=
github.com/digitalocean/godo v1.84.1 h1:VgPsuxhrO9pUygvij6qOhqXfAkxAsDZYRpmjSDMEaHo=
github.com/digitalocean/godo v1.84.1/go.mod h1:BPCqvwbjbGqxuUnIKB4EvS/AX7IDnNmt5fwvIkWo+ew=
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
github.com/docker/cli v0.0.0-20191017083524-a8ff7f821017/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/cli v20.10.17+incompatible h1:eO2KS7ZFeov5UJeaDmIs1NFEDRf32PaqRpvoEkKBy5M=
Expand Down
59 changes: 53 additions & 6 deletions integration/floating_ip_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ var _ = suite("compute/floating-ip/create", func(t *testing.T, when spec.G, it s
reqBody, err := ioutil.ReadAll(req.Body)
expect.NoError(err)

responseJSON := floatingIPCreateResponse
matchedRequest := floatingIPCreateRequest
if !strings.Contains(string(reqBody), "droplet_id") {
matchedRequest = floatingIPRegionCreateRequest
responseJSON = floatingIPCreateRegionResponse
}

expect.JSONEq(matchedRequest, string(reqBody))

w.Write([]byte(floatingIPCreateResponse))
w.Write([]byte(responseJSON))
default:
dump, err := httputil.DumpRequest(req, true)
if err != nil {
Expand All @@ -61,7 +63,7 @@ var _ = suite("compute/floating-ip/create", func(t *testing.T, when spec.G, it s

})

when("the minimum flags are provided", func() {
when("the droplet-id flag is provided", func() {
it("creates the floating-ip", func() {
aliases := []string{"create", "c"}

Expand All @@ -81,12 +83,38 @@ var _ = suite("compute/floating-ip/create", func(t *testing.T, when spec.G, it s
}
})
})

when("the region and project-id flags are provided", func() {
it("creates the floating-ip", func() {
aliases := []string{"create", "c"}

for _, alias := range aliases {
cmd = exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"compute",
"floating-ip",
alias,
"--region", "nyc3",
"--project-id", "c98374fa-35e2-11ed-870f-c7de97c5d5ed",
)

output, err := cmd.CombinedOutput()
expect.NoError(err, fmt.Sprintf("received error output: %s", output))
expect.Equal(strings.TrimSpace(floatingIPCreateRegionOutput), strings.TrimSpace(string(output)))
}
})
})
})

const (
floatingIPCreateOutput = `
IP Region Droplet ID Droplet Name
45.55.96.47 nyc3 1212 magic-name
IP Region Droplet ID Droplet Name Project ID
45.55.96.47 nyc3 1212 magic-name c98374fa-35e2-11ed-870f-c7de97c5d5ed
`
floatingIPCreateRegionOutput = `
IP Region Droplet ID Droplet Name Project ID
45.55.96.47 nyc3 c98374fa-35e2-11ed-870f-c7de97c5d5ed
`
floatingIPCreateResponse = `
{
Expand All @@ -103,7 +131,26 @@ IP Region Droplet ID Droplet Name
"features": [ "metadata" ],
"available": true
},
"locked": false
"locked": false,
"project_id": "c98374fa-35e2-11ed-870f-c7de97c5d5ed"
},
"links": {}
}
`
floatingIPCreateRegionResponse = `
{
"reserved_ip": {
"ip": "45.55.96.47",
"droplet": null,
"region": {
"name": "New York 3",
"slug": "nyc3",
"sizes": [ "s-32vcpu-192gb" ],
"features": [ "metadata" ],
"available": true
},
"locked": false,
"project_id": "c98374fa-35e2-11ed-870f-c7de97c5d5ed"
},
"links": {}
}
Expand All @@ -112,6 +159,6 @@ IP Region Droplet ID Droplet Name
{"droplet_id":1212}
`
floatingIPRegionCreateRequest = `
{"region":"newark"}
{"region":"nyc3","project_id":"c98374fa-35e2-11ed-870f-c7de97c5d5ed"}
`
)
7 changes: 4 additions & 3 deletions integration/floating_ip_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ var _ = suite("compute/floating-ip/get", func(t *testing.T, when spec.G, it spec

const (
floatingIPGetOutput = `
IP Region Droplet ID Droplet Name
1.1.1.1 nyc3
IP Region Droplet ID Droplet Name Project ID
1.1.1.1 nyc3 c98374fa-35e2-11ed-870f-c7de97c5d5ed
`
floatingIPGetResponse = `
{
Expand All @@ -89,7 +89,8 @@ IP Region Droplet ID Droplet Name
"features": [ "metadata" ],
"available": true
},
"locked": false
"locked": false,
"project_id": "c98374fa-35e2-11ed-870f-c7de97c5d5ed"
}
}
`
Expand Down
14 changes: 8 additions & 6 deletions integration/floating_ip_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ var _ = suite("compute/floating-ip/list", func(t *testing.T, when spec.G, it spe

const (
floatingIPListOutput = `
IP Region Droplet ID Droplet Name
8.8.8.8 nyc3 8888 hello
1.1.1.1 nyc3 1111
IP Region Droplet ID Droplet Name Project ID
8.8.8.8 nyc3 8888 hello c98374fa-35e2-11ed-870f-c7de97c5d5ed
1.1.1.1 nyc3 476dea88-35ea-11ed-8e93-f7eb94d49952
`
floatingIPListResponse = `
{
Expand All @@ -90,19 +90,21 @@ IP Region Droplet ID Droplet Name
"features": [ "metadata" ],
"available": true
},
"locked": false
"locked": false,
"project_id": "c98374fa-35e2-11ed-870f-c7de97c5d5ed"
},
{
"ip": "1.1.1.1",
"droplet": {"id": 1111},
"droplet":null,
"region": {
"name": "New York 3",
"slug": "nyc3",
"sizes": [ "s-1vcpu-1gb" ],
"features": [ "metadata" ],
"available": true
},
"locked": false
"locked": false,
"project_id": "476dea88-35ea-11ed-8e93-f7eb94d49952"
}
],
"links": {},
Expand Down
7 changes: 4 additions & 3 deletions integration/projects_resources_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ ID Name Public IPv4 Private IPv4 Public IPv6 Memor
5555 some-droplet-name 0 0 0 some-region-slug some-distro some-image-name active yes remotes some-volume-id
`
projectsResourcesGetFloatingIPOutput = `
IP Region Droplet ID Droplet Name
45.55.96.47 nyc3
IP Region Droplet ID Droplet Name Project ID
45.55.96.47 nyc3 c98374fa-35e2-11ed-870f-c7de97c5d5ed
`
projectsResourcesGetFloatingIPResponse = `
{
Expand All @@ -224,7 +224,8 @@ IP Region Droplet ID Droplet Name
"features": [ "metadata" ],
"available": true
},
"locked": false
"locked": false,
"project_id": "c98374fa-35e2-11ed-870f-c7de97c5d5ed"
}
}
`
Expand Down
Loading

0 comments on commit f5e6747

Please sign in to comment.