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

fix(instance): add ID suffix to organization field #861

Merged
merged 3 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ USAGE:

EXAMPLES:
Create an image named 'foobar' for x86_64 instances from the given root_volume ID (root_volume ID needs to be a snapshot UUID)
scw instance image create
scw instance image create name=foobar root-volume=11111111-1111-1111-1111-111111111111 arch=x86_64

ARGS:
[name=<generated>] Name of the image
snapshot-id UUID of the snapshot
arch Architecture of the image (x86_64 | arm)
[default-bootscript] Default bootscript of the image
[additional-volumes.{key}.id] UUID of the volume
[additional-volumes.{key}.name] Name of the volume
[additional-volumes.{key}.size] Disk size of the volume
[additional-volumes.{key}.volume-type] Type of the volume (l_ssd | b_ssd)
[additional-volumes.{key}.organization] Organization ID of the volume
[public] True to create a public image
[organization] Organization ID to use. If none is passed will use default organization ID from the config
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)
[name=<generated>] Name of the image
snapshot-id UUID of the snapshot
arch Architecture of the image (x86_64 | arm)
[default-bootscript] Default bootscript of the image
[additional-volumes.{key}.id] UUID of the volume
[additional-volumes.{key}.name] Name of the volume
[additional-volumes.{key}.size] Disk size of the volume
[additional-volumes.{key}.volume-type] Type of the volume (l_ssd | b_ssd)
[additional-volumes.{key}.organization-id] Organization ID of the volume
[public] True to create a public image
[organization-id] Organization ID to use. If none is passed will use default organization ID from the config
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
-h, --help help for create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ EXAMPLES:
scw instance image list

ARGS:
[name]
[public]
[arch]
[organization]
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)
[name]
[public]
[arch]
[organization-id]
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
-h, --help help for list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ EXAMPLES:
scw instance ip create server=11111111-1111-1111-1111-111111111111

ARGS:
[server] UUID of the server you want to attach the IP to
[tags.{index}] An array of keywords you want to tag this IP with
[organization] Organization ID to use. If none is passed will use default organization ID from the config
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)
[server] UUID of the server you want to attach the IP to
[tags.{index}] An array of keywords you want to tag this IP with
[organization-id] Organization ID to use. If none is passed will use default organization ID from the config
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
-h, --help help for create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ EXAMPLES:
scw instance ip list zone=fr-par-1

ARGS:
[name] Filter on the IP address (Works as a LIKE operation on the IP address)
[organization] The organization ID the IPs are reserved in
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)
[name] Filter on the IP address (Works as a LIKE operation on the IP address)
[organization-id] The organization ID the IPs are reserved in
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
-h, --help help for list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ARGS:
[name=<generated>] Name of the placement group
[policy-mode] (optional | enforced)
[policy-type] (max_availability | low_latency)
[organization] Organization ID to use. If none is passed will use default organization ID from the config
[organization-id] Organization ID to use. If none is passed will use default organization ID from the config
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ EXAMPLES:
scw instance placement-group list name=cluster1

ARGS:
[name] Filter placement groups by name (for eg. "cluster1" will return "cluster100" and "cluster1" but not "foo")
[organization] List only placement groups of this organization
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)
[name] Filter placement groups by name (for eg. "cluster1" will return "cluster100" and "cluster1" but not "foo")
[organization-id] List only placement groups of this organization
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
-h, --help help for list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ARGS:
[stateful=true] Whether the security group is stateful or not
[inbound-default-policy=accept] Default policy for inbound rules (accept | drop)
[outbound-default-policy=accept] Default policy for outbound rules (accept | drop)
[organization] Organization ID to use. If none is passed will use default organization ID from the config
[organization-id] Organization ID to use. If none is passed will use default organization ID from the config
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ EXAMPLES:
scw instance security-group list name=foobar

ARGS:
[name] Name of the security group
[organization] The security group organization ID
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)
[name] Name of the security group
[organization-id] The security group organization ID
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
-h, --help help for list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ARGS:
[commercial-type] List servers of this commercial type
[state] List servers in this state (running | stopped | stopped in place | starting | stopping | locked)
[tags.{index}] List servers with these exact tags
[organization] List only servers of this organization
[organization-id] List only servers of this organization
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ USAGE:

EXAMPLES:
Update the name of a given server
scw instance server update
scw instance server update 11111111-1111-1111-1111-111111111111 name=foobar

Put a given instance in rescue mode (reboot is required to access rescue mode)
scw instance server update
scw instance server update 11111111-1111-1111-1111-111111111111 boot-type=rescue

Overwrite tags of a given server
scw instance server update
scw instance server update 11111111-1111-1111-1111-111111111111 tags.0=foo tags.1=bar

Enable IPv6 on a given server
scw instance server update
scw instance server update 11111111-1111-1111-1111-111111111111 enable-ipv6=true

Apply the given security group to a given server
scw instance server update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ EXAMPLES:
ARGS:
[name=<generated>] Name of the snapshot
volume-id UUID of the volume
[organization] Organization ID to use. If none is passed will use default organization ID from the config
[organization-id] Organization ID to use. If none is passed will use default organization ID from the config
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ EXAMPLES:
scw instance snapshot list zone=fr-par-1

ARGS:
[name]
[organization]
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)
[name]
[organization-id]
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
-h, --help help for list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ EXAMPLES:
scw instance volume create volume-type=l_ssd base-volume=00112233-4455-6677-8899-aabbccddeeff

ARGS:
[name]
[volume-type] (l_ssd | b_ssd)
[size]
[base-volume]
[base-snapshot]
[organization] Organization ID to use. If none is passed will use default organization ID from the config
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)
[name]
[volume-type] (l_ssd | b_ssd)
[size]
[base-volume]
[base-snapshot]
[organization-id] Organization ID to use. If none is passed will use default organization ID from the config
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
-h, --help help for create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ EXAMPLES:
scw instance volume list volume-type=b_ssd name=foobar

ARGS:
[volume-type] Filter by volume type (l_ssd | b_ssd)
[name] Filter volume by name (for eg. "vol" will return "myvolume" but not "data")
[organization] Filter volume by organization
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)
[volume-type] Filter by volume type (l_ssd | b_ssd)
[name] Filter volume by name (for eg. "vol" will return "myvolume" but not "data")
[organization-id] Filter volume by organization
[zone] Zone to target. If none is passed will use default zone from the config (fr-par-1 | nl-ams-1)

FLAGS:
-h, --help help for list
Expand Down
16 changes: 14 additions & 2 deletions internal/core/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@ func newObjectWithForcedJSONTags(t reflect.Type) interface{} {
structFieldsCopy := []reflect.StructField(nil)
for i := 0; i < t.NumField(); i++ {
fieldCopy := t.Field(i)
fieldCopy.Tag = reflect.StructTag(`json:"` + strings.ReplaceAll(strcase.ToBashArg(fieldCopy.Name), "-", "_") + `"`)
structFieldsCopy = append(structFieldsCopy, fieldCopy)
if fieldCopy.Anonymous {
anonymousType := fieldCopy.Type
if anonymousType.Kind() == reflect.Ptr {
anonymousType = anonymousType.Elem()
}
for i := 0; i < anonymousType.NumField(); i++ {
fieldCopy := anonymousType.Field(i)
fieldCopy.Tag = reflect.StructTag(`json:"` + strings.ReplaceAll(strcase.ToBashArg(fieldCopy.Name), "-", "_") + `"`)
structFieldsCopy = append(structFieldsCopy, fieldCopy)
}
} else {
fieldCopy.Tag = reflect.StructTag(`json:"` + strings.ReplaceAll(strcase.ToBashArg(fieldCopy.Name), "-", "_") + `"`)
structFieldsCopy = append(structFieldsCopy, fieldCopy)
}
}
return reflect.New(reflect.StructOf(structFieldsCopy)).Interface()
}
Expand Down
19 changes: 19 additions & 0 deletions internal/namespaces/instance/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
)

var (
oldOrganizationFieldName = "organization"
newOrganizationFieldName = "organization-id"
)

// GetCommands returns instance commands.
//
// This function:
Expand All @@ -29,6 +34,7 @@ func GetCommands() *core.Commands {
human.RegisterMarshalerFunc(instance.GetServerResponse{}, getServerResponseMarshalerFunc)
human.RegisterMarshalerFunc(instance.Bootscript{}, bootscriptMarshalerFunc)

cmds.MustFind("instance", "server", "list").Override(serverListBuilder)
cmds.MustFind("instance", "server", "update").Override(serverUpdateBuilder)

cmds.Merge(core.NewCommands(
Expand All @@ -55,6 +61,9 @@ func GetCommands() *core.Commands {
//
human.RegisterMarshalerFunc(instance.CreateIPResponse{}, marshallNestedField("IP"))

cmds.MustFind("instance", "ip", "create").Override(ipCreateBuilder)
cmds.MustFind("instance", "ip", "list").Override(ipListBuilder)

//
// Image
//
Expand All @@ -68,6 +77,9 @@ func GetCommands() *core.Commands {
//
human.RegisterMarshalerFunc(instance.CreateSnapshotResponse{}, marshallNestedField("Snapshot"))

cmds.MustFind("instance", "snapshot", "create").Override(snapshotCreateBuilder)
cmds.MustFind("instance", "snapshot", "list").Override(snapshotListBuilder)

//
// Volume
//
Expand All @@ -76,13 +88,18 @@ func GetCommands() *core.Commands {
human.RegisterMarshalerFunc(instance.VolumeSummary{}, volumeSummaryMarshalerFunc)
human.RegisterMarshalerFunc(map[string]*instance.Volume{}, volumeMapMarshalerFunc)

cmds.MustFind("instance", "volume", "create").Override(volumeCreateBuilder)
cmds.MustFind("instance", "volume", "list").Override(volumeListBuilder)

//
// Security Group
//
human.RegisterMarshalerFunc(instance.CreateSecurityGroupResponse{}, marshallNestedField("SecurityGroup"))
human.RegisterMarshalerFunc(instance.SecurityGroupPolicy(0), human.EnumMarshalFunc(securityGroupPolicyMarshalSpecs))

cmds.MustFind("instance", "security-group", "create").Override(securityGroupCreateBuilder)
cmds.MustFind("instance", "security-group", "get").Override(securityGroupGetBuilder)
cmds.MustFind("instance", "security-group", "list").Override(securityGroupListBuilder)
cmds.MustFind("instance", "security-group", "delete").Override(securityGroupDeleteBuilder)

cmds.Merge(core.NewCommands(
Expand All @@ -101,7 +118,9 @@ func GetCommands() *core.Commands {
//
human.RegisterMarshalerFunc(instance.CreatePlacementGroupResponse{}, marshallNestedField("PlacementGroup"))

cmds.MustFind("instance", "placement-group", "create").Override(placementGroupCreateBuilder)
cmds.MustFind("instance", "placement-group", "get").Override(placementGroupGetBuilder)
cmds.MustFind("instance", "placement-group", "list").Override(placementGroupListBuilder)

//
// User Data
Expand Down
23 changes: 21 additions & 2 deletions internal/namespaces/instance/v1/custom_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ func imageCreateBuilder(c *core.Command) *core.Command {
*instance.CreateImageRequest
AdditionalVolumes map[string]*instance.VolumeTemplate
SnapshotID string
OrganizationID string
}

c.ArgSpecs.GetByName("extra-volumes.{key}.id").Name = "additional-volumes.{key}.id"
c.ArgSpecs.GetByName("extra-volumes.{key}.name").Name = "additional-volumes.{key}.name"
c.ArgSpecs.GetByName("extra-volumes.{key}.size").Name = "additional-volumes.{key}.size"
c.ArgSpecs.GetByName("extra-volumes.{key}.volume-type").Name = "additional-volumes.{key}.volume-type"
c.ArgSpecs.GetByName("extra-volumes.{key}.organization").Name = "additional-volumes.{key}.organization"
c.ArgSpecs.GetByName("extra-volumes.{key}.organization").Name = "additional-volumes.{key}.organization-id"

c.ArgSpecs.GetByName("root-volume").Name = "snapshot-id"

c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName

c.ArgsType = reflect.TypeOf(customCreateImageRequest{})

c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) {
Expand All @@ -41,6 +44,7 @@ func imageCreateBuilder(c *core.Command) *core.Command {
request := args.CreateImageRequest
request.RootVolume = args.SnapshotID
request.ExtraVolumes = make(map[string]*instance.VolumeTemplate)
request.Organization = args.OrganizationID

// Extra volumes need to start at volumeIndex 1.
volumeIndex := 1
Expand All @@ -58,6 +62,14 @@ func imageCreateBuilder(c *core.Command) *core.Command {
// imageListBuilder list the images for a given organization.
// A call to GetServer(..) with the ID contained in Image.FromServer retrieves more information about the server.
func imageListBuilder(c *core.Command) *core.Command {
type customListImageRequest struct {
*instance.ListImagesRequest
OrganizationID *string
}

c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this line is duplicated in a lot of places, we could add a small function renameOrganizationIDArgSpec(c.ArgSpecs)

c.ArgsType = reflect.TypeOf(customListImageRequest{})

c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
// customImage is based on instance.Image, with additional information about the server
type customImage struct {
Expand All @@ -80,7 +92,14 @@ func imageListBuilder(c *core.Command) *core.Command {
}

// Get images
req := argsI.(*instance.ListImagesRequest)
args := argsI.(*customListImageRequest)

if args.ListImagesRequest == nil {
args.ListImagesRequest = &instance.ListImagesRequest{}
}

req := args.ListImagesRequest
req.Organization = args.OrganizationID
req.Public = scw.BoolPtr(false)
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
Expand Down
Loading