Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Primary IP Support #393

Merged
merged 1 commit into from
Jun 28, 2022
Merged
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
6 changes: 4 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include:
- project: "hc/backend/deploy-tools"
- project: "cloud/backend/deploy-tools"
file: "ci/test/golang.yml"

variables:
Expand Down Expand Up @@ -27,4 +27,6 @@ test:tests:
- hc-bladerunner
artifacts:
reports:
cobertura: coverage.xml
coverage_report:
coverage_format: cobertura
path: coverage.xml
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/fatih/structs v1.1.0
github.com/golang/mock v1.6.0
github.com/guptarohit/asciigraph v0.5.1
github.com/hetznercloud/hcloud-go v1.34.0
github.com/hetznercloud/hcloud-go v1.35.0
github.com/pelletier/go-toml v1.8.1
github.com/rjeczalik/interfaces v0.1.1
github.com/spf13/cobra v1.1.3
Expand All @@ -23,6 +23,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
Expand All @@ -34,12 +35,14 @@ require (
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/tools v0.1.1 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.26.0-rc.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

go 1.17
go 1.18
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/boumenot/gocover-cobertura v1.1.0 h1:nqMsp1zONyd3Bz98gscF2CpMupSCY0BQA4nmLhkcoYQ=
github.com/boumenot/gocover-cobertura v1.1.0/go.mod h1:fz7ly8dslE42VRR5ZWLt2OHGDHjkTiA2oNvKgJEjLT0=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down Expand Up @@ -93,8 +92,9 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -129,8 +129,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hetznercloud/hcloud-go v1.34.0 h1:yCmlDl+S9LDDuk0PkStn7XT/DAlBquE5WS4BEnDE5Xc=
github.com/hetznercloud/hcloud-go v1.34.0/go.mod h1:ztUc4lPyGRKJDJ6i8evK4kwAlYO0aZkVAMoZwX9nSjQ=
github.com/hetznercloud/hcloud-go v1.35.0 h1:sduXOrWM0/sJXwBty7EQd7+RXEJh5+CsAGQmHshChFg=
github.com/hetznercloud/hcloud-go v1.35.0/go.mod h1:mepQwR6va27S3UQthaEPGS86jtzSY9xWL1e9dyxXpgA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down Expand Up @@ -304,6 +304,7 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -347,6 +348,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hetznercloud/cli/internal/cmd/location"
"github.com/hetznercloud/cli/internal/cmd/network"
"github.com/hetznercloud/cli/internal/cmd/placementgroup"
"github.com/hetznercloud/cli/internal/cmd/primaryip"
"github.com/hetznercloud/cli/internal/cmd/server"
"github.com/hetznercloud/cli/internal/cmd/servertype"
"github.com/hetznercloud/cli/internal/cmd/sshkey"
Expand Down Expand Up @@ -55,6 +56,7 @@ func NewRootCommand(state *state.State, client hcapi2.Client) *cobra.Command {
certificate.NewCommand(state, client),
firewall.NewCommand(state, client),
placementgroup.NewCommand(state, client),
primaryip.NewCommand(state, client),
)
cmd.PersistentFlags().Duration("poll-interval", 500*time.Millisecond, "Interval at which to poll information, for example action progress")
return cmd
Expand Down
1 change: 0 additions & 1 deletion internal/cmd/floatingip/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func newAssignCommand(cli *state.State) *cobra.Command {
PreRunE: cli.EnsureToken,
RunE: cli.Wrap(runAssign),
}
cmd.MarkFlagRequired("server")
return cmd
}

Expand Down
68 changes: 68 additions & 0 deletions internal/cmd/primaryip/assign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package primaryip

import (
"context"
"fmt"
"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

var AssignCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "assign [FLAGS] PRIMARYIP",
Short: "Assign a Primary IP to an assignee (usually a server)",
Args: cobra.ExactArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(
cmpl.SuggestCandidatesF(client.PrimaryIP().Names),
),
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
cmd.Flags().String("server", "", "Name or ID of the server")
cmd.RegisterFlagCompletionFunc("server", cmpl.SuggestCandidatesF(client.Server().Names))
cmd.MarkFlagRequired("server")
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
idOrName := args[0]
primaryIP, _, err := client.PrimaryIP().Get(ctx, idOrName)
if err != nil {
return err
}
if primaryIP == nil {
return fmt.Errorf("Primary IP not found: %v", idOrName)
}

serverIDOrName, _ := cmd.Flags().GetString("server")

server, _, err := client.Server().Get(ctx, serverIDOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("server not found: %s", serverIDOrName)
}
opts := hcloud.PrimaryIPAssignOpts{
ID: primaryIP.ID,
AssigneeType: "server",
AssigneeID: server.ID,
}

action, _, err := client.PrimaryIP().Assign(ctx, opts)
if err != nil {
return err
}

if err := actionWaiter.ActionProgress(ctx, action); err != nil {
return err
}

fmt.Printf("Primary IP %d assigned to %s %d\n", opts.ID, opts.AssigneeType, opts.AssigneeID)
return nil
},
}
64 changes: 64 additions & 0 deletions internal/cmd/primaryip/assign_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package primaryip

import (
"context"
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/hetznercloud/cli/internal/testutil"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/stretchr/testify/assert"
)

func TestAssign(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := AssignCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter)
action := &hcloud.Action{ID: 1}
fx.ExpectEnsureToken()
var (
server = hcloud.Server{
ID: 15,
Name: "my server",
}
)

fx.Client.ServerClient.EXPECT().
Get(gomock.Any(), fmt.Sprintf("%d", server.ID)).
Return(&server, nil, nil)
fx.Client.PrimaryIPClient.EXPECT().
Get(
gomock.Any(),
"13",
).
Return(
&hcloud.PrimaryIP{ID: 13},
&hcloud.Response{},
nil,
)
fx.Client.PrimaryIPClient.EXPECT().
Assign(
gomock.Any(),
hcloud.PrimaryIPAssignOpts{
ID: 13,
AssigneeType: "server",
AssigneeID: 15,
},
).
Return(
action,
&hcloud.Response{},
nil,
)

fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), action).Return(nil)

out, err := fx.Run(cmd, []string{"13", "--server", "15"})

expOut := "Primary IP 13 assigned to server 15\n"

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}
63 changes: 63 additions & 0 deletions internal/cmd/primaryip/changedns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package primaryip

import (
"context"
"fmt"
"github.com/hetznercloud/cli/internal/cmd/cmpl"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

var ChangeDNSCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "set-rdns [FLAGS] PRIMARYIP",
Short: "Change the reverse DNS from a Primary IP",
Args: cobra.ExactArgs(1),
TraverseChildren: true,
ValidArgsFunction: cmpl.SuggestArgs(
cmpl.SuggestCandidatesF(client.PrimaryIP().Names),
),
DisableFlagsInUseLine: true,
}
cmd.Flags().String("hostname", "", "Hostname to set as a reverse DNS PTR entry (required)")
cmd.MarkFlagRequired("hostname")
cmd.Flags().String("ip", "", "IP address for which the reverse DNS entry should be set (required)")
cmd.MarkFlagRequired("ip")
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
idOrName := args[0]
primaryIP, _, err := client.PrimaryIP().Get(ctx, idOrName)
if err != nil {
return err
}
if primaryIP == nil {
return fmt.Errorf("Primary IP not found: %v", idOrName)
}

DNSPtr, _ := cmd.Flags().GetString("hostname")
ip, _ := cmd.Flags().GetString("ip")
opts := hcloud.PrimaryIPChangeDNSPtrOpts{
ID: primaryIP.ID,
DNSPtr: DNSPtr,
IP: ip,
}

action, _, err := client.PrimaryIP().ChangeDNSPtr(ctx, opts)
if err != nil {
return err
}

if err := actionWaiter.ActionProgress(ctx, action); err != nil {
return err
}

fmt.Printf("Primary IP %d DNS pointer: %s associated to %s\n", opts.ID, opts.DNSPtr, opts.IP)
return nil
},
}
53 changes: 53 additions & 0 deletions internal/cmd/primaryip/changedns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package primaryip

import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/hetznercloud/cli/internal/testutil"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/stretchr/testify/assert"
)

func TestChangeDNS(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := ChangeDNSCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter)
action := &hcloud.Action{ID: 1}
fx.ExpectEnsureToken()
fx.Client.PrimaryIPClient.EXPECT().
Get(
gomock.Any(),
"13",
).
Return(
&hcloud.PrimaryIP{ID: 13},
&hcloud.Response{},
nil,
)
fx.Client.PrimaryIPClient.EXPECT().
ChangeDNSPtr(
gomock.Any(),
hcloud.PrimaryIPChangeDNSPtrOpts{
ID: 13,
DNSPtr: "server.your-host.de",
IP: "192.168.0.1",
},
).
Return(
action,
&hcloud.Response{},
nil,
)

fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), action).Return(nil)

out, err := fx.Run(cmd, []string{"--hostname=server.your-host.de", "--ip=192.168.0.1", "13"})

expOut := "Primary IP 13 DNS pointer: server.your-host.de associated to 192.168.0.1\n"

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}
Loading