-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lukas Kämmerling <[email protected]> Co-authored-by: Adrian Huber <[email protected]>
- Loading branch information
1 parent
2ab6137
commit 4b8ed4d
Showing
35 changed files
with
1,250 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package placementgroup | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"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 CreateCmd = base.Cmd{ | ||
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create FLAGS", | ||
Short: "Create a placement group", | ||
} | ||
cmd.Flags().String("name", "", "Name") | ||
cmd.MarkFlagRequired("name") | ||
|
||
cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") | ||
|
||
cmd.Flags().String("type", "", "Type of the placement group") | ||
cmd.MarkFlagRequired("type") | ||
return cmd | ||
}, | ||
Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error { | ||
name, _ := cmd.Flags().GetString("name") | ||
labels, _ := cmd.Flags().GetStringToString("label") | ||
placementGroupType, _ := cmd.Flags().GetString("type") | ||
|
||
opts := hcloud.PlacementGroupCreateOpts{ | ||
Name: name, | ||
Labels: labels, | ||
Type: hcloud.PlacementGroupType(placementGroupType), | ||
} | ||
|
||
result, _, err := client.PlacementGroup().Create(ctx, opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Placement group %d created\n", result.PlacementGroup.ID) | ||
|
||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package placementgroup_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/hetznercloud/cli/internal/cmd/placementgroup" | ||
"github.com/hetznercloud/cli/internal/testutil" | ||
"github.com/hetznercloud/hcloud-go/hcloud" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreate(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := placementgroup.CreateCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
opts := hcloud.PlacementGroupCreateOpts{ | ||
Name: "my Placement Group", | ||
Labels: map[string]string{}, | ||
Type: hcloud.PlacementGroupTypeSpread, | ||
} | ||
|
||
placementGroup := hcloud.PlacementGroup{ | ||
ID: 897, | ||
Name: opts.Name, | ||
Created: time.Now(), | ||
Labels: opts.Labels, | ||
Type: opts.Type, | ||
} | ||
|
||
fx.Client.PlacementGroupClient.EXPECT(). | ||
Create(gomock.Any(), opts). | ||
Return(hcloud.PlacementGroupCreateResult{PlacementGroup: &placementGroup, Action: nil}, nil, nil) | ||
|
||
out, err := fx.Run(cmd, []string{"--name", placementGroup.Name, "--type", string(placementGroup.Type)}) | ||
|
||
expOut := `Placement group 897 created | ||
` | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package placementgroup | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/base" | ||
"github.com/hetznercloud/cli/internal/hcapi2" | ||
"github.com/hetznercloud/hcloud-go/hcloud" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var DeleteCmd = base.DeleteCmd{ | ||
ResourceNameSingular: "placement group", | ||
ShortDescription: "Delete a placement group", | ||
NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, | ||
Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { | ||
return client.PlacementGroup().Get(ctx, idOrName) | ||
}, | ||
Delete: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { | ||
placementGroup := resource.(*hcloud.PlacementGroup) | ||
if _, err := client.PlacementGroup().Delete(ctx, placementGroup); err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package placementgroup_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/hetznercloud/cli/internal/cmd/placementgroup" | ||
"github.com/hetznercloud/cli/internal/testutil" | ||
"github.com/hetznercloud/hcloud-go/hcloud" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDelete(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := placementgroup.DeleteCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer) | ||
fx.ExpectEnsureToken() | ||
|
||
placementGroup := hcloud.PlacementGroup{ | ||
ID: 897, | ||
Name: "my Placement Group", | ||
Created: time.Now(), | ||
Labels: map[string]string{"key": "value"}, | ||
Servers: []int{4711, 4712}, | ||
Type: hcloud.PlacementGroupTypeSpread, | ||
} | ||
|
||
fx.Client.PlacementGroupClient.EXPECT(). | ||
Get(gomock.Any(), placementGroup.Name). | ||
Return(&placementGroup, nil, nil) | ||
fx.Client.PlacementGroupClient.EXPECT(). | ||
Delete(gomock.Any(), &placementGroup) | ||
|
||
_, err := fx.Run(cmd, []string{placementGroup.Name}) | ||
|
||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package placementgroup | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/dustin/go-humanize" | ||
"github.com/hetznercloud/cli/internal/cmd/base" | ||
"github.com/hetznercloud/cli/internal/cmd/util" | ||
"github.com/hetznercloud/cli/internal/hcapi2" | ||
"github.com/hetznercloud/hcloud-go/hcloud" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var DescribeCmd = base.DescribeCmd{ | ||
ResourceNameSingular: "placement group", | ||
ShortDescription: "Describe a placement group", | ||
JSONKeyGetByID: "placement group", | ||
JSONKeyGetByName: "placement groups", | ||
NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, | ||
Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { | ||
return client.PlacementGroup().Get(ctx, idOrName) | ||
}, | ||
PrintText: func(_ context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { | ||
placementGroup := resource.(*hcloud.PlacementGroup) | ||
|
||
fmt.Printf("ID:\t\t%d\n", placementGroup.ID) | ||
fmt.Printf("Name:\t\t%s\n", placementGroup.Name) | ||
fmt.Printf("Created:\t%s (%s)\n", util.Datetime(placementGroup.Created), humanize.Time(placementGroup.Created)) | ||
|
||
fmt.Print("Labels:\n") | ||
if len(placementGroup.Labels) == 0 { | ||
fmt.Print(" No labels\n") | ||
} else { | ||
for key, value := range placementGroup.Labels { | ||
fmt.Printf(" %s: %s\n", key, value) | ||
} | ||
} | ||
|
||
fmt.Print("Servers:\n") | ||
for _, serverID := range placementGroup.Servers { | ||
fmt.Printf(" - Server ID:\t\t%d\n", serverID) | ||
fmt.Printf(" Server Name:\t%s\n", client.Server().ServerName(serverID)) | ||
} | ||
|
||
fmt.Printf("Type:\t\t%s\n", placementGroup.Type) | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package placementgroup_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/dustin/go-humanize" | ||
"github.com/golang/mock/gomock" | ||
"github.com/hetznercloud/cli/internal/cmd/placementgroup" | ||
"github.com/hetznercloud/cli/internal/cmd/util" | ||
"github.com/hetznercloud/cli/internal/testutil" | ||
"github.com/hetznercloud/hcloud-go/hcloud" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDescribe(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := placementgroup.DescribeCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer) | ||
fx.ExpectEnsureToken() | ||
|
||
placementGroup := hcloud.PlacementGroup{ | ||
ID: 897, | ||
Name: "my Placement Group", | ||
Created: time.Date(2021, 07, 23, 10, 0, 0, 0, time.UTC), | ||
Labels: map[string]string{"key": "value"}, | ||
Servers: []int{4711, 4712}, | ||
Type: hcloud.PlacementGroupTypeSpread, | ||
} | ||
|
||
fx.Client.PlacementGroupClient.EXPECT(). | ||
Get(gomock.Any(), placementGroup.Name). | ||
Return(&placementGroup, nil, nil) | ||
fx.Client.ServerClient.EXPECT(). | ||
ServerName(4711). | ||
Return("server1") | ||
fx.Client.ServerClient.EXPECT(). | ||
ServerName(4712). | ||
Return("server2") | ||
|
||
out, err := fx.Run(cmd, []string{placementGroup.Name}) | ||
|
||
expOut := fmt.Sprintf(`ID: 897 | ||
Name: my Placement Group | ||
Created: %s (%s) | ||
Labels: | ||
key: value | ||
Servers: | ||
- Server ID: 4711 | ||
Server Name: server1 | ||
- Server ID: 4712 | ||
Server Name: server2 | ||
Type: spread | ||
`, util.Datetime(placementGroup.Created), | ||
humanize.Time(placementGroup.Created), | ||
) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package placementgroup | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/base" | ||
"github.com/hetznercloud/cli/internal/hcapi2" | ||
"github.com/hetznercloud/hcloud-go/hcloud" | ||
) | ||
|
||
var LabelCmds = base.LabelCmds{ | ||
ResourceNameSingular: "placement group", | ||
ShortDescriptionAdd: "Add a label to a placement group", | ||
ShortDescriptionRemove: "Remove a label from a placement group", | ||
NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, | ||
LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.PlacementGroup().LabelKeys }, | ||
FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int, error) { | ||
placementGroup, _, err := client.PlacementGroup().Get(ctx, idOrName) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
if placementGroup == nil { | ||
return nil, 0, fmt.Errorf("placement group not found: %s", idOrName) | ||
} | ||
return placementGroup.Labels, placementGroup.ID, nil | ||
}, | ||
SetLabels: func(ctx context.Context, client hcapi2.Client, id int, labels map[string]string) error { | ||
opts := hcloud.PlacementGroupUpdateOpts{ | ||
Labels: labels, | ||
} | ||
_, _, err := client.PlacementGroup().Update(ctx, &hcloud.PlacementGroup{ID: id}, opts) | ||
return err | ||
}, | ||
} |
Oops, something went wrong.