From 9680909160dd726a42935198a09905002d365dd6 Mon Sep 17 00:00:00 2001 From: Patrik Cyvoct Date: Thu, 9 Apr 2020 08:02:00 +0200 Subject: [PATCH 1/3] fix(instance): add ID suffix to organization field Signed-off-by: Patrik Cyvoct --- ...-instance-image-create-usage.stderr.golden | 26 ++++---- ...ge-instance-image-list-usage.stderr.golden | 10 +-- ...age-instance-ip-create-usage.stderr.golden | 8 +-- ...usage-instance-ip-list-usage.stderr.golden | 6 +- ...placement-group-create-usage.stderr.golden | 2 +- ...e-placement-group-list-usage.stderr.golden | 6 +- ...-security-group-create-usage.stderr.golden | 2 +- ...ce-security-group-list-usage.stderr.golden | 6 +- ...e-instance-server-list-usage.stderr.golden | 2 +- ...instance-server-update-usage.stderr.golden | 8 +-- ...stance-snapshot-create-usage.stderr.golden | 2 +- ...instance-snapshot-list-usage.stderr.golden | 6 +- ...instance-volume-create-usage.stderr.golden | 14 ++--- ...e-instance-volume-list-usage.stderr.golden | 8 +-- internal/core/reflect.go | 16 ++++- internal/namespaces/instance/v1/custom.go | 19 ++++++ .../namespaces/instance/v1/custom_image.go | 23 ++++++- internal/namespaces/instance/v1/custom_ip.go | 60 ++++++++++++++++++ .../instance/v1/custom_placement_group.go | 51 ++++++++++++++++ .../instance/v1/custom_security_group.go | 52 ++++++++++++++++ .../namespaces/instance/v1/custom_server.go | 25 ++++++++ .../namespaces/instance/v1/custom_snapshot.go | 61 +++++++++++++++++++ .../namespaces/instance/v1/custom_volume.go | 55 +++++++++++++++++ 23 files changed, 411 insertions(+), 57 deletions(-) create mode 100644 internal/namespaces/instance/v1/custom_ip.go create mode 100644 internal/namespaces/instance/v1/custom_snapshot.go diff --git a/cmd/scw/testdata/test-all-usage-instance-image-create-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-image-create-usage.stderr.golden index 0d72e0d5c1..c7b6404946 100644 --- a/cmd/scw/testdata/test-all-usage-instance-image-create-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-image-create-usage.stderr.golden @@ -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=] 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=] 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 diff --git a/cmd/scw/testdata/test-all-usage-instance-image-list-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-image-list-usage.stderr.golden index 90b8ddc85d..2f54a67b2d 100644 --- a/cmd/scw/testdata/test-all-usage-instance-image-list-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-image-list-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-instance-ip-create-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-ip-create-usage.stderr.golden index cdd0cd68ff..463abf8415 100644 --- a/cmd/scw/testdata/test-all-usage-instance-ip-create-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-ip-create-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-instance-ip-list-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-ip-list-usage.stderr.golden index 5496d4dd82..42b684a454 100644 --- a/cmd/scw/testdata/test-all-usage-instance-ip-list-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-ip-list-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-instance-placement-group-create-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-placement-group-create-usage.stderr.golden index 1dbf2176d2..12914bba8f 100644 --- a/cmd/scw/testdata/test-all-usage-instance-placement-group-create-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-placement-group-create-usage.stderr.golden @@ -26,7 +26,7 @@ ARGS: [name=] 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: diff --git a/cmd/scw/testdata/test-all-usage-instance-placement-group-list-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-placement-group-list-usage.stderr.golden index 12585f543e..1318273782 100644 --- a/cmd/scw/testdata/test-all-usage-instance-placement-group-list-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-placement-group-list-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-instance-security-group-create-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-security-group-create-usage.stderr.golden index 7c6b058703..cc35267e89 100644 --- a/cmd/scw/testdata/test-all-usage-instance-security-group-create-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-security-group-create-usage.stderr.golden @@ -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: diff --git a/cmd/scw/testdata/test-all-usage-instance-security-group-list-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-security-group-list-usage.stderr.golden index c7bebd526e..8e55ad84c5 100644 --- a/cmd/scw/testdata/test-all-usage-instance-security-group-list-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-security-group-list-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-instance-server-list-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-server-list-usage.stderr.golden index 266afbe3ef..627283ab05 100644 --- a/cmd/scw/testdata/test-all-usage-instance-server-list-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-server-list-usage.stderr.golden @@ -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: diff --git a/cmd/scw/testdata/test-all-usage-instance-server-update-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-server-update-usage.stderr.golden index 75382873ad..96f1c7b80c 100644 --- a/cmd/scw/testdata/test-all-usage-instance-server-update-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-server-update-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-instance-snapshot-create-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-snapshot-create-usage.stderr.golden index dee3449757..bbc15e3d0b 100644 --- a/cmd/scw/testdata/test-all-usage-instance-snapshot-create-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-snapshot-create-usage.stderr.golden @@ -16,7 +16,7 @@ EXAMPLES: ARGS: [name=] 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: diff --git a/cmd/scw/testdata/test-all-usage-instance-snapshot-list-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-snapshot-list-usage.stderr.golden index 2fae7e595e..b03821e99f 100644 --- a/cmd/scw/testdata/test-all-usage-instance-snapshot-list-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-snapshot-list-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-instance-volume-create-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-volume-create-usage.stderr.golden index bbfa45ce24..f513c6dce0 100644 --- a/cmd/scw/testdata/test-all-usage-instance-volume-create-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-volume-create-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-instance-volume-list-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-volume-list-usage.stderr.golden index 53850669d9..4132dda01d 100644 --- a/cmd/scw/testdata/test-all-usage-instance-volume-list-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-volume-list-usage.stderr.golden @@ -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 diff --git a/internal/core/reflect.go b/internal/core/reflect.go index e112ebd84d..006dc104bb 100644 --- a/internal/core/reflect.go +++ b/internal/core/reflect.go @@ -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 == true { + 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() } diff --git a/internal/namespaces/instance/v1/custom.go b/internal/namespaces/instance/v1/custom.go index d98728aa02..dd7e518d36 100644 --- a/internal/namespaces/instance/v1/custom.go +++ b/internal/namespaces/instance/v1/custom.go @@ -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: @@ -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( @@ -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 // @@ -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 // @@ -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( @@ -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 diff --git a/internal/namespaces/instance/v1/custom_image.go b/internal/namespaces/instance/v1/custom_image.go index 01be4b7cca..42ab88c617 100644 --- a/internal/namespaces/instance/v1/custom_image.go +++ b/internal/namespaces/instance/v1/custom_image.go @@ -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) { @@ -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 @@ -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 + 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 { @@ -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) diff --git a/internal/namespaces/instance/v1/custom_ip.go b/internal/namespaces/instance/v1/custom_ip.go new file mode 100644 index 0000000000..6428a32f76 --- /dev/null +++ b/internal/namespaces/instance/v1/custom_ip.go @@ -0,0 +1,60 @@ +package instance + +import ( + "context" + "reflect" + + "github.com/scaleway/scaleway-cli/internal/core" + "github.com/scaleway/scaleway-sdk-go/api/instance/v1" +) + +// Builders + +func ipCreateBuilder(c *core.Command) *core.Command { + type customCreateIPRequest struct { + *instance.CreateIPRequest + OrganizationID string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customCreateIPRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customCreateIPRequest) + + if args.CreateIPRequest == nil { + args.CreateIPRequest = &instance.CreateIPRequest{} + } + request := args.CreateIPRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + + return c +} + +func ipListBuilder(c *core.Command) *core.Command { + type customListIPsRequest struct { + *instance.ListIPsRequest + OrganizationID *string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customListIPsRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customListIPsRequest) + + if args.ListIPsRequest == nil { + args.ListIPsRequest = &instance.ListIPsRequest{} + } + request := args.ListIPsRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + return c +} diff --git a/internal/namespaces/instance/v1/custom_placement_group.go b/internal/namespaces/instance/v1/custom_placement_group.go index db17c22bb9..54985e42a9 100644 --- a/internal/namespaces/instance/v1/custom_placement_group.go +++ b/internal/namespaces/instance/v1/custom_placement_group.go @@ -2,6 +2,7 @@ package instance import ( "context" + "reflect" "github.com/scaleway/scaleway-cli/internal/core" "github.com/scaleway/scaleway-sdk-go/api/instance/v1" @@ -48,3 +49,53 @@ func placementGroupGetBuilder(c *core.Command) *core.Command { return c } + +func placementGroupCreateBuilder(c *core.Command) *core.Command { + type customCreatePlacementGroupRequest struct { + *instance.CreatePlacementGroupRequest + OrganizationID string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customCreatePlacementGroupRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customCreatePlacementGroupRequest) + + if args.CreatePlacementGroupRequest == nil { + args.CreatePlacementGroupRequest = &instance.CreatePlacementGroupRequest{} + } + + request := args.CreatePlacementGroupRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + return c +} + +func placementGroupListBuilder(c *core.Command) *core.Command { + type customListPlacementGroupsRequest struct { + *instance.ListPlacementGroupsRequest + OrganizationID *string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customListPlacementGroupsRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customListPlacementGroupsRequest) + + if args.ListPlacementGroupsRequest == nil { + args.ListPlacementGroupsRequest = &instance.ListPlacementGroupsRequest{} + } + + request := args.ListPlacementGroupsRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + return c +} diff --git a/internal/namespaces/instance/v1/custom_security_group.go b/internal/namespaces/instance/v1/custom_security_group.go index 65e9c163c7..17f75e792e 100644 --- a/internal/namespaces/instance/v1/custom_security_group.go +++ b/internal/namespaces/instance/v1/custom_security_group.go @@ -157,6 +157,32 @@ type customSecurityGroupResponse struct { Rules []*instance.SecurityGroupRule } +func securityGroupCreateBuilder(c *core.Command) *core.Command { + type customCreateSecurityGroupRequest struct { + *instance.CreateSecurityGroupRequest + OrganizationID string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customCreateSecurityGroupRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customCreateSecurityGroupRequest) + + if args.CreateSecurityGroupRequest == nil { + args.CreateSecurityGroupRequest = &instance.CreateSecurityGroupRequest{} + } + + request := args.CreateSecurityGroupRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + + return c +} + func securityGroupGetBuilder(c *core.Command) *core.Command { c.Run = func(ctx context.Context, argsI interface{}) (interface{}, error) { req := argsI.(*instance.GetSecurityGroupRequest) @@ -184,6 +210,32 @@ func securityGroupGetBuilder(c *core.Command) *core.Command { return c } +func securityGroupListBuilder(c *core.Command) *core.Command { + type customListSecurityGroupsRequest struct { + *instance.ListSecurityGroupsRequest + OrganizationID *string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customListSecurityGroupsRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customListSecurityGroupsRequest) + + if args.ListSecurityGroupsRequest == nil { + args.ListSecurityGroupsRequest = &instance.ListSecurityGroupsRequest{} + } + + request := args.ListSecurityGroupsRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + + return c +} + func securityGroupDeleteBuilder(c *core.Command) *core.Command { c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { res, originalErr := runner(ctx, argsI) diff --git a/internal/namespaces/instance/v1/custom_server.go b/internal/namespaces/instance/v1/custom_server.go index 04bcfcb033..2f807bcde2 100644 --- a/internal/namespaces/instance/v1/custom_server.go +++ b/internal/namespaces/instance/v1/custom_server.go @@ -173,6 +173,31 @@ func bootscriptMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, erro // Builders // +func serverListBuilder(c *core.Command) *core.Command { + type customListServersRequest struct { + *instance.ListServersRequest + OrganizationID *string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customListServersRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customListServersRequest) + + if args.ListServersRequest == nil { + args.ListServersRequest = &instance.ListServersRequest{} + } + + request := args.ListServersRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + return c +} + func serverUpdateBuilder(c *core.Command) *core.Command { type instanceUpdateServerRequestCustom struct { *instance.UpdateServerRequest diff --git a/internal/namespaces/instance/v1/custom_snapshot.go b/internal/namespaces/instance/v1/custom_snapshot.go new file mode 100644 index 0000000000..260d562d91 --- /dev/null +++ b/internal/namespaces/instance/v1/custom_snapshot.go @@ -0,0 +1,61 @@ +package instance + +import ( + "context" + "reflect" + + "github.com/scaleway/scaleway-cli/internal/core" + "github.com/scaleway/scaleway-sdk-go/api/instance/v1" +) + +// Builders + +func snapshotCreateBuilder(c *core.Command) *core.Command { + type customCreateSnapshotRequest struct { + *instance.CreateSnapshotRequest + OrganizationID string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customCreateSnapshotRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customCreateSnapshotRequest) + + if args.CreateSnapshotRequest == nil { + args.CreateSnapshotRequest = &instance.CreateSnapshotRequest{} + } + + request := args.CreateSnapshotRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + return c +} + +func snapshotListBuilder(c *core.Command) *core.Command { + type customListSnapshotsRequest struct { + *instance.ListSnapshotsRequest + OrganizationID *string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customListSnapshotsRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customListSnapshotsRequest) + + if args.ListSnapshotsRequest == nil { + args.ListSnapshotsRequest = &instance.ListSnapshotsRequest{} + } + + request := args.ListSnapshotsRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + return c +} diff --git a/internal/namespaces/instance/v1/custom_volume.go b/internal/namespaces/instance/v1/custom_volume.go index a55f63c3cf..e97103cf8a 100644 --- a/internal/namespaces/instance/v1/custom_volume.go +++ b/internal/namespaces/instance/v1/custom_volume.go @@ -1,9 +1,12 @@ package instance import ( + "context" "fmt" + "reflect" "github.com/fatih/color" + "github.com/scaleway/scaleway-cli/internal/core" "github.com/scaleway/scaleway-cli/internal/human" "github.com/scaleway/scaleway-sdk-go/api/instance/v1" ) @@ -30,3 +33,55 @@ func volumeMapMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error volumes := i.(map[string]*instance.Volume) return fmt.Sprintf("%v", len(volumes)), nil } + +// Builders + +func volumeCreateBuilder(c *core.Command) *core.Command { + type customCreateVolumeRequest struct { + *instance.CreateVolumeRequest + OrganizationID string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customCreateVolumeRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customCreateVolumeRequest) + + if args.CreateVolumeRequest == nil { + args.CreateVolumeRequest = &instance.CreateVolumeRequest{} + } + + request := args.CreateVolumeRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + return c +} + +func volumeListBuilder(c *core.Command) *core.Command { + type customListVolumesRequest struct { + *instance.ListVolumesRequest + OrganizationID *string + } + + c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + + c.ArgsType = reflect.TypeOf(customListVolumesRequest{}) + + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) { + args := argsI.(*customListVolumesRequest) + + if args.ListVolumesRequest == nil { + args.ListVolumesRequest = &instance.ListVolumesRequest{} + } + + request := args.ListVolumesRequest + request.Organization = args.OrganizationID + + return runner(ctx, request) + }) + return c +} From 1f4b3837520ef2a51ed141c6df1af0c0c0f8c2f8 Mon Sep 17 00:00:00 2001 From: Patrik Cyvoct Date: Sat, 11 Apr 2020 12:44:51 +0200 Subject: [PATCH 2/3] fix golang ci lint Signed-off-by: Patrik Cyvoct --- internal/core/reflect.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/core/reflect.go b/internal/core/reflect.go index 006dc104bb..cfc8f274a5 100644 --- a/internal/core/reflect.go +++ b/internal/core/reflect.go @@ -16,7 +16,7 @@ func newObjectWithForcedJSONTags(t reflect.Type) interface{} { structFieldsCopy := []reflect.StructField(nil) for i := 0; i < t.NumField(); i++ { fieldCopy := t.Field(i) - if fieldCopy.Anonymous == true { + if fieldCopy.Anonymous { anonymousType := fieldCopy.Type if anonymousType.Kind() == reflect.Ptr { anonymousType = anonymousType.Elem() From 39a97d2f1100fe7fbef7d69b6c26741a348d3d68 Mon Sep 17 00:00:00 2001 From: Patrik Cyvoct Date: Tue, 14 Apr 2020 11:44:20 +0200 Subject: [PATCH 3/3] fix review Signed-off-by: Patrik Cyvoct --- internal/namespaces/instance/v1/custom.go | 5 +++++ internal/namespaces/instance/v1/custom_image.go | 5 +++-- internal/namespaces/instance/v1/custom_ip.go | 4 ++-- internal/namespaces/instance/v1/custom_placement_group.go | 4 ++-- internal/namespaces/instance/v1/custom_security_group.go | 4 ++-- internal/namespaces/instance/v1/custom_server.go | 2 +- internal/namespaces/instance/v1/custom_snapshot.go | 4 ++-- internal/namespaces/instance/v1/custom_volume.go | 4 ++-- 8 files changed, 19 insertions(+), 13 deletions(-) diff --git a/internal/namespaces/instance/v1/custom.go b/internal/namespaces/instance/v1/custom.go index dd7e518d36..a005234bdc 100644 --- a/internal/namespaces/instance/v1/custom.go +++ b/internal/namespaces/instance/v1/custom.go @@ -14,6 +14,11 @@ var ( newOrganizationFieldName = "organization-id" ) +// helpers +func renameOrganizationIDArgSpec(argSpecs core.ArgSpecs) { + argSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName +} + // GetCommands returns instance commands. // // This function: diff --git a/internal/namespaces/instance/v1/custom_image.go b/internal/namespaces/instance/v1/custom_image.go index 42ab88c617..73e1602176 100644 --- a/internal/namespaces/instance/v1/custom_image.go +++ b/internal/namespaces/instance/v1/custom_image.go @@ -34,7 +34,7 @@ func imageCreateBuilder(c *core.Command) *core.Command { c.ArgSpecs.GetByName("root-volume").Name = "snapshot-id" - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customCreateImageRequest{}) @@ -67,7 +67,8 @@ func imageListBuilder(c *core.Command) *core.Command { OrganizationID *string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) + c.ArgsType = reflect.TypeOf(customListImageRequest{}) c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) { diff --git a/internal/namespaces/instance/v1/custom_ip.go b/internal/namespaces/instance/v1/custom_ip.go index 6428a32f76..325722576b 100644 --- a/internal/namespaces/instance/v1/custom_ip.go +++ b/internal/namespaces/instance/v1/custom_ip.go @@ -16,7 +16,7 @@ func ipCreateBuilder(c *core.Command) *core.Command { OrganizationID string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customCreateIPRequest{}) @@ -41,7 +41,7 @@ func ipListBuilder(c *core.Command) *core.Command { OrganizationID *string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customListIPsRequest{}) diff --git a/internal/namespaces/instance/v1/custom_placement_group.go b/internal/namespaces/instance/v1/custom_placement_group.go index 54985e42a9..b2995882fb 100644 --- a/internal/namespaces/instance/v1/custom_placement_group.go +++ b/internal/namespaces/instance/v1/custom_placement_group.go @@ -56,7 +56,7 @@ func placementGroupCreateBuilder(c *core.Command) *core.Command { OrganizationID string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customCreatePlacementGroupRequest{}) @@ -81,7 +81,7 @@ func placementGroupListBuilder(c *core.Command) *core.Command { OrganizationID *string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customListPlacementGroupsRequest{}) diff --git a/internal/namespaces/instance/v1/custom_security_group.go b/internal/namespaces/instance/v1/custom_security_group.go index 17f75e792e..0a8f791235 100644 --- a/internal/namespaces/instance/v1/custom_security_group.go +++ b/internal/namespaces/instance/v1/custom_security_group.go @@ -163,7 +163,7 @@ func securityGroupCreateBuilder(c *core.Command) *core.Command { OrganizationID string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customCreateSecurityGroupRequest{}) @@ -216,7 +216,7 @@ func securityGroupListBuilder(c *core.Command) *core.Command { OrganizationID *string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customListSecurityGroupsRequest{}) diff --git a/internal/namespaces/instance/v1/custom_server.go b/internal/namespaces/instance/v1/custom_server.go index 2f807bcde2..53ae6aab0e 100644 --- a/internal/namespaces/instance/v1/custom_server.go +++ b/internal/namespaces/instance/v1/custom_server.go @@ -179,7 +179,7 @@ func serverListBuilder(c *core.Command) *core.Command { OrganizationID *string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customListServersRequest{}) diff --git a/internal/namespaces/instance/v1/custom_snapshot.go b/internal/namespaces/instance/v1/custom_snapshot.go index 260d562d91..2bc7e6dc92 100644 --- a/internal/namespaces/instance/v1/custom_snapshot.go +++ b/internal/namespaces/instance/v1/custom_snapshot.go @@ -16,7 +16,7 @@ func snapshotCreateBuilder(c *core.Command) *core.Command { OrganizationID string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customCreateSnapshotRequest{}) @@ -41,7 +41,7 @@ func snapshotListBuilder(c *core.Command) *core.Command { OrganizationID *string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customListSnapshotsRequest{}) diff --git a/internal/namespaces/instance/v1/custom_volume.go b/internal/namespaces/instance/v1/custom_volume.go index e97103cf8a..a187c2c7c3 100644 --- a/internal/namespaces/instance/v1/custom_volume.go +++ b/internal/namespaces/instance/v1/custom_volume.go @@ -42,7 +42,7 @@ func volumeCreateBuilder(c *core.Command) *core.Command { OrganizationID string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customCreateVolumeRequest{}) @@ -67,7 +67,7 @@ func volumeListBuilder(c *core.Command) *core.Command { OrganizationID *string } - c.ArgSpecs.GetByName(oldOrganizationFieldName).Name = newOrganizationFieldName + renameOrganizationIDArgSpec(c.ArgSpecs) c.ArgsType = reflect.TypeOf(customListVolumesRequest{})