diff --git a/.github/scripts/check-creds-settings.sh b/.github/scripts/check-creds-settings.sh index 6523324a4..ad3142585 100755 --- a/.github/scripts/check-creds-settings.sh +++ b/.github/scripts/check-creds-settings.sh @@ -14,4 +14,8 @@ if [ -z "$OSC_REGION" ]; then echo "OSC_REGION var is not set, please fix" exit_code=1 fi +if [ -z "$OMI_ID" ]; then + echo "OMI_ID var is not set, please fix" + exit_code=1 +fi exit $exit_code \ No newline at end of file diff --git a/.github/workflows/pull-request-v1.yml b/.github/workflows/pull-request-v1.yml index 4458ac598..d5cf5c8dc 100644 --- a/.github/workflows/pull-request-v1.yml +++ b/.github/workflows/pull-request-v1.yml @@ -29,6 +29,7 @@ jobs: OSC_ACCESS_KEY: ${{ secrets.OSC_ACCESS_KEY }} OSC_SECRET_KEY: ${{ secrets.OSC_SECRET_KEY }} OSC_REGION: ${{ secrets.OSC_REGION }} + OMI_ID: ${{ secrets.OMI_ID }} run: make go-test regeneration-test: runs-on: ubuntu-20.04 diff --git a/.osc-patches/23.03.01-Fix-BasePath-region.patch b/.osc-patches/23.03.01-Fix-BasePath-region.patch new file mode 100644 index 000000000..08a6d6e8c --- /dev/null +++ b/.osc-patches/23.03.01-Fix-BasePath-region.patch @@ -0,0 +1,28 @@ +From 53fc47b4030cc7d24eeaea67adef3c280b495c0d Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Jutteau?= +Date: Wed, 1 Mar 2023 12:32:28 +0100 +Subject: [PATCH 1/2] Fix BasePath region +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Signed-off-by: Jérôme Jutteau +--- + osc/config_file.go | 1 + + 1 file changed, 1 insertion(+) + +diff --git a/osc/config_file.go b/osc/config_file.go +index b35e9537..66efe5bf 100644 +--- a/osc/config_file.go ++++ b/osc/config_file.go +@@ -139,6 +139,7 @@ func (configFile *ConfigFile) Configuration(profileName string) (*Configuration, + } + + config := NewConfiguration() ++ config.BasePath = fmt.Sprintf("https://api.%s.outscale.com/api/v1", region) + config.Servers = []ServerConfiguration{ + { + Url: url, +-- +2.38.1 + diff --git a/.osc-patches/23.03.01-Fix-config-env-BasePath.patch b/.osc-patches/23.03.01-Fix-config-env-BasePath.patch new file mode 100644 index 000000000..c88c56577 --- /dev/null +++ b/.osc-patches/23.03.01-Fix-config-env-BasePath.patch @@ -0,0 +1,38 @@ +From d9039cddadd92d851831912529e1545c5883e5cb Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Jutteau?= +Date: Wed, 1 Mar 2023 12:39:48 +0100 +Subject: [PATCH 2/2] Fix config env BasePath +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Signed-off-by: Jérôme Jutteau +--- + osc/config_env.go | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/osc/config_env.go b/osc/config_env.go +index 9fcd634f..1d4af8ce 100644 +--- a/osc/config_env.go ++++ b/osc/config_env.go +@@ -29,6 +29,7 @@ package osc + import ( + "context" + "errors" ++ "fmt" + "os" + ) + +@@ -74,6 +75,9 @@ func (configEnv *ConfigEnv) Configuration() (*Configuration, error) { + } + } else { + config = NewConfiguration() ++ if configEnv.Region != nil { ++ config.BasePath = fmt.Sprintf("https://api.%s.outscale.com/api/v1", *configEnv.Region) ++ } + default_region := "eu-west-2" + config.Servers = []ServerConfiguration{ + { +-- +2.38.1 + diff --git a/examples/debug_test.go b/examples/debug_test.go index 034f2394e..45eef8d80 100644 --- a/examples/debug_test.go +++ b/examples/debug_test.go @@ -34,8 +34,9 @@ package osc_test import ( "context" "fmt" - "github.com/outscale/osc-sdk-go/osc" "os" + + "github.com/outscale/osc-sdk-go/osc" ) /* @@ -45,17 +46,24 @@ A quick example which show how to enable SDK debuging in case you have to see wh This examples just list existing volumes and shows HTTP details. */ func ExampleDebug() { - config := osc.NewConfiguration() + configEnv := osc.NewConfigEnv() + config, err := configEnv.Configuration() + fmt.Fprintf(os.Stderr, "BasePath: %s\n", config.BasePath) - config.Debug = true // <-- may be useful + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot load configuration from environement variables") + os.Exit(1) + } + ctx, err := configEnv.Context(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot set context from environement variables") + os.Exit(1) + } + config.Debug = true client := osc.NewAPIClient(config) - auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{ - AccessKey: os.Getenv("OSC_ACCESS_KEY"), - SecretKey: os.Getenv("OSC_SECRET_KEY"), - }) - read, httpRes, err := client.VolumeApi.ReadVolumes(auth, nil) + read, httpRes, err := client.VolumeApi.ReadVolumes(ctx, nil) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading volumes") if httpRes != nil { diff --git a/examples/image_test.go b/examples/image_test.go index ee9d35c8f..ef723a75c 100644 --- a/examples/image_test.go +++ b/examples/image_test.go @@ -41,13 +41,23 @@ import ( /* Image Example: list machine images and get details */ func ExampleImage() { - client := osc.NewAPIClient(osc.NewConfiguration()) - auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{ - AccessKey: os.Getenv("OSC_ACCESS_KEY"), - SecretKey: os.Getenv("OSC_SECRET_KEY"), - }) + configEnv := osc.NewConfigEnv() + config, err := configEnv.Configuration() - read, httpRes, err := client.ImageApi.ReadImages(auth, nil) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot load configuration from environement variables") + os.Exit(1) + } + ctx, err := configEnv.Context(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot set context from environement variables") + os.Exit(1) + } + + config.Debug = true + client := osc.NewAPIClient(config) + + read, httpRes, err := client.ImageApi.ReadImages(ctx, nil) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading images") if httpRes != nil { diff --git a/examples/keypair_test.go b/examples/keypair_test.go index 84c50c751..5f3c0c518 100644 --- a/examples/keypair_test.go +++ b/examples/keypair_test.go @@ -49,13 +49,21 @@ import ( - Delete keypair */ func ExampleKeypair() { - client := osc.NewAPIClient(osc.NewConfiguration()) - auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{ - AccessKey: os.Getenv("OSC_ACCESS_KEY"), - SecretKey: os.Getenv("OSC_SECRET_KEY"), - }) + configEnv := osc.NewConfigEnv() + config, err := configEnv.Configuration() - read, httpRes, err := client.KeypairApi.ReadKeypairs(auth, nil) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot load configuration from environement variables") + os.Exit(1) + } + ctx, err := configEnv.Context(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot set context from environement variables") + os.Exit(1) + } + client := osc.NewAPIClient(config) + + read, httpRes, err := client.KeypairApi.ReadKeypairs(ctx, nil) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading keypairs") if httpRes != nil { @@ -79,7 +87,7 @@ func ExampleKeypair() { //PublicKey: publicKey, }), } - creation, httpRes, err := client.KeypairApi.CreateKeypair(auth, &creationOpts) + creation, httpRes, err := client.KeypairApi.CreateKeypair(ctx, &creationOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while creating keypair ") if httpRes != nil { @@ -98,7 +106,7 @@ func ExampleKeypair() { KeypairName: keypairName, }), } - _, httpRes, err = client.KeypairApi.DeleteKeypair(auth, &deletionOpts) + _, httpRes, err = client.KeypairApi.DeleteKeypair(ctx, &deletionOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while deleting keypair ") if httpRes != nil { diff --git a/examples/load_balancer_test.go b/examples/load_balancer_test.go index 528f4c86e..a13f9047a 100644 --- a/examples/load_balancer_test.go +++ b/examples/load_balancer_test.go @@ -51,17 +51,24 @@ import ( - Delete new load balancer */ func ExampleLoadBalancer() { - loadBalancerName := "OscSdkExample-" + RandomString(10) - loadBalancerSubRegion := "eu-west-2a" - config := osc.NewConfiguration() - config.Debug = false + configEnv := osc.NewConfigEnv() + config, err := configEnv.Configuration() + + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot load configuration from environement variables") + os.Exit(1) + } + ctx, err := configEnv.Context(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot set context from environement variables") + os.Exit(1) + } client := osc.NewAPIClient(config) - auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{ - AccessKey: os.Getenv("OSC_ACCESS_KEY"), - SecretKey: os.Getenv("OSC_SECRET_KEY"), - }) - read, httpRes, err := client.LoadBalancerApi.ReadLoadBalancers(auth, nil) + loadBalancerName := "OscSdkExample-" + RandomString(10) + loadBalancerSubRegion := fmt.Sprintf("%sa", os.Getenv("OSC_REGION")) + + read, httpRes, err := client.LoadBalancerApi.ReadLoadBalancers(ctx, nil) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading load balancers") if httpRes != nil { @@ -91,7 +98,7 @@ func ExampleLoadBalancer() { SubregionNames: []string{loadBalancerSubRegion}, }), } - creation, httpRes, err := client.LoadBalancerApi.CreateLoadBalancer(auth, &creationOpts) + creation, httpRes, err := client.LoadBalancerApi.CreateLoadBalancer(ctx, &creationOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while creating load balancer ") if httpRes != nil { @@ -110,7 +117,7 @@ func ExampleLoadBalancer() { }, }), } - read, httpRes, err = client.LoadBalancerApi.ReadLoadBalancers(auth, &readOpts) + read, httpRes, err = client.LoadBalancerApi.ReadLoadBalancers(ctx, &readOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while reading load balancers ") if httpRes != nil { @@ -130,7 +137,7 @@ func ExampleLoadBalancer() { LoadBalancerName: creation.LoadBalancer.LoadBalancerName, }), } - _, httpRes, err = client.LoadBalancerApi.DeleteLoadBalancer(auth, &deletionOpts) + _, httpRes, err = client.LoadBalancerApi.DeleteLoadBalancer(ctx, &deletionOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while deleting load balancer ") if httpRes != nil { diff --git a/examples/public_ip_test.go b/examples/public_ip_test.go index d52e42525..0bedd8585 100644 --- a/examples/public_ip_test.go +++ b/examples/public_ip_test.go @@ -34,9 +34,10 @@ package osc_test import ( "context" "fmt" + "os" + "github.com/antihax/optional" "github.com/outscale/osc-sdk-go/osc" - "os" ) /* @@ -48,13 +49,20 @@ import ( - Delete Public IP */ func ExamplePublicIp() { - client := osc.NewAPIClient(osc.NewConfiguration()) - auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{ - AccessKey: os.Getenv("OSC_ACCESS_KEY"), - SecretKey: os.Getenv("OSC_SECRET_KEY"), - }) + configEnv := osc.NewConfigEnv() + config, err := configEnv.Configuration() + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot load configuration from environement variables") + os.Exit(1) + } + ctx, err := configEnv.Context(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot set context from environement variables") + os.Exit(1) + } + client := osc.NewAPIClient(config) - read, httpRes, err := client.PublicIpApi.ReadPublicIps(auth, nil) + read, httpRes, err := client.PublicIpApi.ReadPublicIps(ctx, nil) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading public ips") if httpRes != nil { @@ -70,7 +78,7 @@ func ExamplePublicIp() { println("Creating new Public Ip") creationOpts := osc.CreatePublicIpOpts{} - creation, httpRes, err := client.PublicIpApi.CreatePublicIp(auth, &creationOpts) + creation, httpRes, err := client.PublicIpApi.CreatePublicIp(ctx, &creationOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while creating Public IP ") if httpRes != nil { @@ -90,7 +98,7 @@ func ExamplePublicIp() { }, }), } - read, httpRes, err = client.PublicIpApi.ReadPublicIps(auth, &readOpts) + read, httpRes, err = client.PublicIpApi.ReadPublicIps(ctx, &readOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while reading public ip ") if httpRes != nil { @@ -110,7 +118,7 @@ func ExamplePublicIp() { PublicIpId: creation.PublicIp.PublicIpId, }), } - _, httpRes, err = client.PublicIpApi.DeletePublicIp(auth, &deletionOpts) + _, httpRes, err = client.PublicIpApi.DeletePublicIp(ctx, &deletionOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while deleting public ip ") if httpRes != nil { diff --git a/examples/security_group_test.go b/examples/security_group_test.go index 347b86ccf..0ff7573b2 100644 --- a/examples/security_group_test.go +++ b/examples/security_group_test.go @@ -50,13 +50,20 @@ import ( - Delete Security Group */ func ExampleSecurityGroup() { - client := osc.NewAPIClient(osc.NewConfiguration()) - auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{ - AccessKey: os.Getenv("OSC_ACCESS_KEY"), - SecretKey: os.Getenv("OSC_SECRET_KEY"), - }) + configEnv := osc.NewConfigEnv() + config, err := configEnv.Configuration() + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot load configuration from environement variables") + os.Exit(1) + } + ctx, err := configEnv.Context(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot set context from environement variables") + os.Exit(1) + } + client := osc.NewAPIClient(config) - read, httpRes, err := client.SecurityGroupApi.ReadSecurityGroups(auth, nil) + read, httpRes, err := client.SecurityGroupApi.ReadSecurityGroups(ctx, nil) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading security groups") if httpRes != nil { @@ -77,7 +84,7 @@ func ExampleSecurityGroup() { Description: "osc-sdk-go security group example", }), } - creation, httpRes, err := client.SecurityGroupApi.CreateSecurityGroup(auth, &creationOpts) + creation, httpRes, err := client.SecurityGroupApi.CreateSecurityGroup(ctx, &creationOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while creating security group ") if httpRes != nil { @@ -100,7 +107,7 @@ func ExampleSecurityGroup() { ToPortRange: 22, }), } - _, httpRes, err = client.SecurityGroupRuleApi.CreateSecurityGroupRule(auth, &ruleAddOpts) + _, httpRes, err = client.SecurityGroupRuleApi.CreateSecurityGroupRule(ctx, &ruleAddOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while creating security group rule") if httpRes != nil { @@ -119,7 +126,7 @@ func ExampleSecurityGroup() { }, }), } - read, httpRes, err = client.SecurityGroupApi.ReadSecurityGroups(auth, &readOpts) + read, httpRes, err = client.SecurityGroupApi.ReadSecurityGroups(ctx, &readOpts) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading security groups") if httpRes != nil { @@ -143,7 +150,7 @@ func ExampleSecurityGroup() { ToPortRange: 22, }), } - _, httpRes, err = client.SecurityGroupRuleApi.DeleteSecurityGroupRule(auth, &ruleDelOpts) + _, httpRes, err = client.SecurityGroupRuleApi.DeleteSecurityGroupRule(ctx, &ruleDelOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while deleting security group rule") if httpRes != nil { @@ -160,7 +167,7 @@ func ExampleSecurityGroup() { SecurityGroupId: sgID, }), } - _, httpRes, err = client.SecurityGroupApi.DeleteSecurityGroup(auth, &deletionOpts) + _, httpRes, err = client.SecurityGroupApi.DeleteSecurityGroup(ctx, &deletionOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while deleting security group ") if httpRes != nil { diff --git a/examples/snapshot_test.go b/examples/snapshot_test.go index 34a0a6367..332762f94 100644 --- a/examples/snapshot_test.go +++ b/examples/snapshot_test.go @@ -48,13 +48,20 @@ import ( - Delete new snapshot */ func ExampleSnapshot() { - client := osc.NewAPIClient(osc.NewConfiguration()) - auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{ - AccessKey: os.Getenv("OSC_ACCESS_KEY"), - SecretKey: os.Getenv("OSC_SECRET_KEY"), - }) + configEnv := osc.NewConfigEnv() + config, err := configEnv.Configuration() + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot load configuration from environement variables") + os.Exit(1) + } + ctx, err := configEnv.Context(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot set context from environement variables") + os.Exit(1) + } + client := osc.NewAPIClient(config) - read, httpRes, err := client.SnapshotApi.ReadSnapshots(auth, nil) + read, httpRes, err := client.SnapshotApi.ReadSnapshots(ctx, nil) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading snapshots") if httpRes != nil { diff --git a/examples/tag_test.go b/examples/tag_test.go index 567cc3052..73323e292 100644 --- a/examples/tag_test.go +++ b/examples/tag_test.go @@ -49,11 +49,18 @@ Example of adding tags on a resource (e.g. a virtual volume) 5. delete volume */ func ExampleTag() { - client := osc.NewAPIClient(osc.NewConfiguration()) - auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{ - AccessKey: os.Getenv("OSC_ACCESS_KEY"), - SecretKey: os.Getenv("OSC_SECRET_KEY"), - }) + configEnv := osc.NewConfigEnv() + config, err := configEnv.Configuration() + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot load configuration from environement variables") + os.Exit(1) + } + ctx, err := configEnv.Context(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot set context from environement variables") + os.Exit(1) + } + client := osc.NewAPIClient(config) println("Creating 10GB GP2 volume") volumeCreationOpts := osc.CreateVolumeOpts{ @@ -61,10 +68,10 @@ func ExampleTag() { osc.CreateVolumeRequest{ Size: 10, VolumeType: "gp2", - SubregionName: "eu-west-2a", + SubregionName: fmt.Sprintf("%sa", os.Getenv("OSC_REGION")), }), } - volumeCreation, httpRes, err := client.VolumeApi.CreateVolume(auth, &volumeCreationOpts) + volumeCreation, httpRes, err := client.VolumeApi.CreateVolume(ctx, &volumeCreationOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while creating volume ") if httpRes != nil { @@ -86,7 +93,7 @@ func ExampleTag() { }, ), } - _, httpRes, err = client.TagApi.CreateTags(auth, &tagCreationOpts) + _, httpRes, err = client.TagApi.CreateTags(ctx, &tagCreationOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while creating tags ") if httpRes != nil { @@ -105,7 +112,7 @@ func ExampleTag() { }, }), } - read, httpRes, err := client.TagApi.ReadTags(auth, &readOpts) + read, httpRes, err := client.TagApi.ReadTags(ctx, &readOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while reading tags ") if httpRes != nil { @@ -129,7 +136,7 @@ func ExampleTag() { }, ), } - _, httpRes, err = client.TagApi.DeleteTags(auth, &tagDeletionOpts) + _, httpRes, err = client.TagApi.DeleteTags(ctx, &tagDeletionOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while removing tag ") if httpRes != nil { @@ -146,7 +153,7 @@ func ExampleTag() { VolumeId: volumeCreation.Volume.VolumeId, }), } - _, httpRes, err = client.VolumeApi.DeleteVolume(auth, &deletionOpts) + _, httpRes, err = client.VolumeApi.DeleteVolume(ctx, &deletionOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while deleting volume ") if httpRes != nil { diff --git a/examples/virtual_machine_test.go b/examples/virtual_machine_test.go index 131d47b37..43f0270e6 100644 --- a/examples/virtual_machine_test.go +++ b/examples/virtual_machine_test.go @@ -51,13 +51,20 @@ import ( Note that to access a virtual machine, you will also need at least to provide a security group with appropriate rules (e.g. TCP port 22) and a keypair at creation. */ func ExampleVirtualMachine() { - client := osc.NewAPIClient(osc.NewConfiguration()) - auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{ - AccessKey: os.Getenv("OSC_ACCESS_KEY"), - SecretKey: os.Getenv("OSC_SECRET_KEY"), - }) + configEnv := osc.NewConfigEnv() + config, err := configEnv.Configuration() + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot load configuration from environement variables") + os.Exit(1) + } + ctx, err := configEnv.Context(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot set context from environement variables") + os.Exit(1) + } + client := osc.NewAPIClient(config) - read, httpRes, err := client.VmApi.ReadVms(auth, nil) + read, httpRes, err := client.VmApi.ReadVms(ctx, nil) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading virtual machines ") if httpRes != nil { @@ -74,11 +81,11 @@ func ExampleVirtualMachine() { creationOpts := osc.CreateVmsOpts{ CreateVmsRequest: optional.NewInterface( osc.CreateVmsRequest{ - ImageId: "ami-68ed4301", + ImageId: os.Getenv("OMI_ID"), VmType: "tinav4.c1r1p1", }), } - creation, httpRes, err := client.VmApi.CreateVms(auth, &creationOpts) + creation, httpRes, err := client.VmApi.CreateVms(ctx, &creationOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while creating virtual machine ") if httpRes != nil { @@ -98,7 +105,7 @@ func ExampleVirtualMachine() { }, }), } - read, httpRes, err = client.VmApi.ReadVms(auth, &readOpts) + read, httpRes, err = client.VmApi.ReadVms(ctx, &readOpts) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading virtual machines") if httpRes != nil { @@ -124,7 +131,7 @@ func ExampleVirtualMachine() { VmIds: []string{VMID}, }), } - _, httpRes, err = client.VmApi.DeleteVms(auth, &deletionOpts) + _, httpRes, err = client.VmApi.DeleteVms(ctx, &deletionOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while deleting virtual machine ") if httpRes != nil { diff --git a/examples/volume_test.go b/examples/volume_test.go index 08eded42b..aec1af65b 100644 --- a/examples/volume_test.go +++ b/examples/volume_test.go @@ -34,9 +34,10 @@ package osc_test import ( "context" "fmt" + "os" + "github.com/antihax/optional" "github.com/outscale/osc-sdk-go/osc" - "os" ) /* @@ -48,13 +49,20 @@ import ( - Delete new volume */ func ExampleVolume() { - client := osc.NewAPIClient(osc.NewConfiguration()) - auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{ - AccessKey: os.Getenv("OSC_ACCESS_KEY"), - SecretKey: os.Getenv("OSC_SECRET_KEY"), - }) + configEnv := osc.NewConfigEnv() + config, err := configEnv.Configuration() + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot load configuration from environement variables") + os.Exit(1) + } + ctx, err := configEnv.Context(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, "Cannot set context from environement variables") + os.Exit(1) + } + client := osc.NewAPIClient(config) - read, httpRes, err := client.VolumeApi.ReadVolumes(auth, nil) + read, httpRes, err := client.VolumeApi.ReadVolumes(ctx, nil) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading volumes") if httpRes != nil { @@ -73,10 +81,10 @@ func ExampleVolume() { osc.CreateVolumeRequest{ Size: 10, VolumeType: "gp2", - SubregionName: "eu-west-2a", + SubregionName: fmt.Sprintf("%sa", os.Getenv("OSC_REGION")), }), } - creation, httpRes, err := client.VolumeApi.CreateVolume(auth, &creationOpts) + creation, httpRes, err := client.VolumeApi.CreateVolume(ctx, &creationOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while creating volume ") if httpRes != nil { @@ -95,7 +103,7 @@ func ExampleVolume() { }, }), } - read, httpRes, err = client.VolumeApi.ReadVolumes(auth, &readOpts) + read, httpRes, err = client.VolumeApi.ReadVolumes(ctx, &readOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while reading volumes ") if httpRes != nil { @@ -117,7 +125,7 @@ func ExampleVolume() { VolumeId: creation.Volume.VolumeId, }), } - _, httpRes, err = client.VolumeApi.DeleteVolume(auth, &deletionOpts) + _, httpRes, err = client.VolumeApi.DeleteVolume(ctx, &deletionOpts) if err != nil { fmt.Fprint(os.Stderr, "Error while deleting volume ") if httpRes != nil { diff --git a/go.mod b/go.mod index 1ee812acf..fe82b7581 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,30 @@ module github.com/outscale/osc-sdk-go +go 1.19 + require ( github.com/antihax/optional v1.0.0 - github.com/aws/aws-sdk-go v1.44.103 - golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 + github.com/aws/aws-sdk-go v1.44.211 + golang.org/x/oauth2 v0.5.0 ) require ( - github.com/golang/protobuf v1.2.0 // indirect + cloud.google.com/go v0.34.0 // indirect + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/golang/protobuf v1.5.2 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect - golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect - google.golang.org/appengine v1.4.0 // indirect + github.com/jmespath/go-jmespath/internal/testify v1.5.1 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.1.0 // indirect + golang.org/x/net v0.7.0 // indirect + golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect + golang.org/x/sys v0.5.0 // indirect + golang.org/x/term v0.5.0 // indirect + golang.org/x/text v0.7.0 // indirect + golang.org/x/tools v0.1.12 // indirect + google.golang.org/appengine v1.6.7 // indirect + google.golang.org/protobuf v1.28.1 // indirect + gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 // indirect + gopkg.in/yaml.v2 v2.2.8 // indirect ) diff --git a/go.sum b/go.sum index b749a4e02..4b223f8f6 100644 --- a/go.sum +++ b/go.sum @@ -3,10 +3,17 @@ github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwc github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/aws/aws-sdk-go v1.44.103 h1:tbhBHKgiZSIUkG8FcHy3wYKpPVvp65Wn7ZiX0B8phpY= github.com/aws/aws-sdk-go v1.44.103/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= +github.com/aws/aws-sdk-go v1.44.211 h1:YNr5DwdzG/8y9Tl0QrPTnC99aFUHgT5hhy6GpnnzHK4= +github.com/aws/aws-sdk-go v1.44.211/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= @@ -15,22 +22,61 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= +golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.5.0 h1:HuArIo48skDwlrvM3sEdHXElYslAMsf3KwRkkW4MC4s= +golang.org/x/oauth2 v0.5.0/go.mod h1:9/XBHVqLaWO3/BRHs5jbpYCnOZVjj5V0ndyaAM7KB4I= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/text v0.3.0/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/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= +google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/osc/config_env.go b/osc/config_env.go index 9fcd634f6..1d4af8cee 100644 --- a/osc/config_env.go +++ b/osc/config_env.go @@ -29,6 +29,7 @@ package osc import ( "context" "errors" + "fmt" "os" ) @@ -74,6 +75,9 @@ func (configEnv *ConfigEnv) Configuration() (*Configuration, error) { } } else { config = NewConfiguration() + if configEnv.Region != nil { + config.BasePath = fmt.Sprintf("https://api.%s.outscale.com/api/v1", *configEnv.Region) + } default_region := "eu-west-2" config.Servers = []ServerConfiguration{ { diff --git a/osc/config_file.go b/osc/config_file.go index b35e95377..66efe5bfe 100644 --- a/osc/config_file.go +++ b/osc/config_file.go @@ -139,6 +139,7 @@ func (configFile *ConfigFile) Configuration(profileName string) (*Configuration, } config := NewConfiguration() + config.BasePath = fmt.Sprintf("https://api.%s.outscale.com/api/v1", region) config.Servers = []ServerConfiguration{ { Url: url,