Best K6 code snippet using cloudapi.retry
resource_alicloud_api_gateway_app.go
Source:resource_alicloud_api_gateway_app.go
1package alicloud2import (3 "strconv"4 "time"5 "github.com/hashicorp/terraform/helper/resource"6 "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"7 "github.com/aliyun/alibaba-cloud-sdk-go/services/cloudapi"8 "github.com/hashicorp/terraform/helper/schema"9 "github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"10)11func resourceAliyunApigatewayApp() *schema.Resource {12 return &schema.Resource{13 Create: resourceAliyunApigatewayAppCreate,14 Read: resourceAliyunApigatewayAppRead,15 Update: resourceAliyunApigatewayAppUpdate,16 Delete: resourceAliyunApigatewayAppDelete,17 Importer: &schema.ResourceImporter{18 State: schema.ImportStatePassthrough,19 },20 Schema: map[string]*schema.Schema{21 "name": {22 Type: schema.TypeString,23 Required: true,24 },25 "description": {26 Type: schema.TypeString,27 Optional: true,28 },29 },30 }31}32func resourceAliyunApigatewayAppCreate(d *schema.ResourceData, meta interface{}) error {33 client := meta.(*connectivity.AliyunClient)34 request := cloudapi.CreateCreateAppRequest()35 request.AppName = d.Get("name").(string)36 if v, exist := d.GetOk("description"); exist {37 request.Description = v.(string)38 }39 request.Description = d.Get("description").(string)40 if err := resource.Retry(5*time.Minute, func() *resource.RetryError {41 raw, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {42 return cloudApiClient.CreateApp(request)43 })44 if err != nil {45 if IsExceptedError(err, RepeatedCommit) {46 return resource.RetryableError(err)47 }48 return resource.NonRetryableError(err)49 }50 addDebug(request.GetActionName(), raw)51 response, _ := raw.(*cloudapi.CreateAppResponse)52 d.SetId(strconv.Itoa(response.AppId))53 return nil54 }); err != nil {55 return WrapErrorf(err, DefaultErrorMsg, "alicloud_apigateway_app", request.GetActionName(), AlibabaCloudSdkGoERROR)56 }57 return resourceAliyunApigatewayAppRead(d, meta)58}59func resourceAliyunApigatewayAppRead(d *schema.ResourceData, meta interface{}) error {60 client := meta.(*connectivity.AliyunClient)61 cloudApiService := CloudApiService{client}62 request := cloudapi.CreateDescribeAppRequest()63 request.AppId = requests.Integer(d.Id())64 if err := resource.Retry(3*time.Second, func() *resource.RetryError {65 object, err := cloudApiService.DescribeApiGatewayApp(d.Id())66 if err != nil {67 if NotFoundError(err) {68 return resource.RetryableError(err)69 }70 return resource.NonRetryableError(err)71 }72 d.Set("name", object.AppName)73 d.Set("description", object.Description)74 return nil75 }); err != nil {76 return WrapError(err)77 }78 return nil79}80func resourceAliyunApigatewayAppUpdate(d *schema.ResourceData, meta interface{}) error {81 client := meta.(*connectivity.AliyunClient)82 if d.HasChange("name") || d.HasChange("description") {83 request := cloudapi.CreateModifyAppRequest()84 request.AppId = requests.Integer(d.Id())85 request.AppName = d.Get("name").(string)86 if v, exist := d.GetOk("description"); exist {87 request.Description = v.(string)88 }89 raw, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {90 return cloudApiClient.ModifyApp(request)91 })92 if err != nil {93 return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)94 }95 addDebug(request.GetActionName(), raw)96 }97 time.Sleep(3 * time.Second)98 return resourceAliyunApigatewayAppRead(d, meta)99}100func resourceAliyunApigatewayAppDelete(d *schema.ResourceData, meta interface{}) error {101 client := meta.(*connectivity.AliyunClient)102 cloudApiService := CloudApiService{client}103 request := cloudapi.CreateDeleteAppRequest()104 request.AppId = requests.Integer(d.Id())105 raw, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {106 return cloudApiClient.DeleteApp(request)107 })108 if err != nil {109 if IsExceptedError(err, NotFoundApp) {110 return nil111 }112 return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)113 }114 addDebug(request.GetActionName(), raw)115 return WrapError(cloudApiService.WaitForApiGatewayApp(d.Id(), Deleted, DefaultTimeout))116}...
resource_alicloud_api_gateway_group.go
Source:resource_alicloud_api_gateway_group.go
1package alicloud2import (3 "fmt"4 "time"5 "github.com/alibaba/terraform-provider/alicloud/connectivity"6 "github.com/aliyun/alibaba-cloud-sdk-go/services/cloudapi"7 "github.com/hashicorp/terraform/helper/resource"8 "github.com/hashicorp/terraform/helper/schema"9)10func resourceAliyunApigatewayGroup() *schema.Resource {11 return &schema.Resource{12 Create: resourceAliyunApigatewayGroupCreate,13 Read: resourceAliyunApigatewayGroupRead,14 Update: resourceAliyunApigatewayGroupUpdate,15 Delete: resourceAliyunApigatewayGroupDelete,16 Importer: &schema.ResourceImporter{17 State: schema.ImportStatePassthrough,18 },19 Schema: map[string]*schema.Schema{20 "name": &schema.Schema{21 Type: schema.TypeString,22 Required: true,23 },24 "description": &schema.Schema{25 Type: schema.TypeString,26 Required: true,27 },28 },29 }30}31func resourceAliyunApigatewayGroupCreate(d *schema.ResourceData, meta interface{}) error {32 client := meta.(*connectivity.AliyunClient)33 args := cloudapi.CreateCreateApiGroupRequest()34 args.GroupName = d.Get("name").(string)35 args.Description = d.Get("description").(string)36 if err := resource.Retry(5*time.Minute, func() *resource.RetryError {37 raw, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {38 return cloudApiClient.CreateApiGroup(args)39 })40 if err != nil {41 if IsExceptedError(err, RepeatedCommit) {42 return resource.RetryableError(fmt.Errorf("Create api group got an error: %#v.", err))43 }44 return resource.NonRetryableError(fmt.Errorf("Create api group got an error: %#v.", err))45 }46 resp, _ := raw.(*cloudapi.CreateApiGroupResponse)47 d.SetId(resp.GroupId)48 return nil49 }); err != nil {50 return fmt.Errorf("Creating apigatway group error: %#v", err)51 }52 return resourceAliyunApigatewayGroupRead(d, meta)53}54func resourceAliyunApigatewayGroupRead(d *schema.ResourceData, meta interface{}) error {55 client := meta.(*connectivity.AliyunClient)56 cloudApiService := CloudApiService{client}57 apiGroup, err := cloudApiService.DescribeApiGroup(d.Id())58 if err != nil {59 if NotFoundError(err) {60 d.SetId("")61 return nil62 }63 return err64 }65 d.Set("name", apiGroup.GroupName)66 d.Set("description", apiGroup.Description)67 return nil68}69func resourceAliyunApigatewayGroupUpdate(d *schema.ResourceData, meta interface{}) error {70 client := meta.(*connectivity.AliyunClient)71 if d.HasChange("name") || d.HasChange("description") {72 req := cloudapi.CreateModifyApiGroupRequest()73 req.Description = d.Get("description").(string)74 req.GroupName = d.Get("name").(string)75 _, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {76 return cloudApiClient.ModifyApiGroup(req)77 })78 if err != nil {79 return fmt.Errorf("ModifyApiGroup got an error: %#v", err)80 }81 }82 return resourceAliyunApigatewayGroupRead(d, meta)83}84func resourceAliyunApigatewayGroupDelete(d *schema.ResourceData, meta interface{}) error {85 client := meta.(*connectivity.AliyunClient)86 cloudApiService := CloudApiService{client}87 req := cloudapi.CreateDeleteApiGroupRequest()88 req.GroupId = d.Id()89 return resource.Retry(5*time.Minute, func() *resource.RetryError {90 _, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {91 return cloudApiClient.DeleteApiGroup(req)92 })93 if err != nil {94 return resource.NonRetryableError(fmt.Errorf("Error deleting ApiGroup failed: %#v", err))95 }96 if _, err := cloudApiService.DescribeApiGroup(d.Id()); err != nil {97 if NotFoundError(err) {98 return nil99 }100 return resource.NonRetryableError(fmt.Errorf("Error describing apiGroup failed when deleting apiGroup: %#v", err))101 }102 return resource.RetryableError(fmt.Errorf("Delete ApiGroup %s timeout.", d.Id()))103 })104}...
retry
Using AI Code Generation
1import (2func main() {3 sess := session.New()4 service := services.GetVirtualGuestService(sess)5 virtualGuest := datatypes.Virtual_Guest{6 Hostname: sl.String("sample"),7 Domain: sl.String("com"),8 MaxMemory: sl.Int(1024),9 StartCpus: sl.Int(1),10 Datacenter: &datatypes.Location{11 Name: sl.String("dal05"),12 },13 OperatingSystemReferenceCode: sl.String("UBUNTU_LATEST"),14 LocalDiskFlag: sl.Bool(false),15 BlockDeviceTemplateGroup: &datatypes.Virtual_Guest_Block_Device_Template_Group{16 GlobalIdentifier: sl.String("YOUR_GLOBAL_ID"),17 },18 }19 receipt, err := service.CreateObject(&virtualGuest)20 if err != nil {21 fmt.Println(err)22 }23 err = service.Id(*receipt.VirtualGuests[0].Id).Mask("id,hostname,domain,primaryIpAddress").GetDedicatedHost()24 if err != nil {25 fmt.Println(err)26 }27 fmt.Printf("Virtual guest created: %s.%s, %s", *receipt.VirtualGuests[0].Hostname, *receipt.VirtualGuests[0].Domain, *receipt.VirtualGuests[0].PrimaryIpAddress)28}29import (30func main() {31 sess := session.New()32 service := services.GetVirtualGuestService(sess)33 virtualGuest := datatypes.Virtual_Guest{34 Hostname: sl.String("sample"),35 Domain: sl.String("
retry
Using AI Code Generation
1import (2func main() {3 cloud, err := cloudapi.NewClient("us-east-1.api.joyentcloud.com", "your account", "your key")4 if err != nil {5 panic(err)6 }7 instance, err := cloud.CreateInstance(&cloudapi.CreateInstanceOpts{8 })9 if err != nil {10 panic(err)11 }12 err = cloud.Retry(10, time.Second, func() (bool, error) {13 instance, err = cloud.GetInstance(instance.Id)14 if err != nil {15 }16 if instance.State != "running" {17 }18 })19 if err != nil {20 panic(err)21 }22 fmt.Printf("instance %s is running23}24import (25func main() {26 cloud, err := cloudapi.NewClient("us-east-1.api.joyentcloud.com", "your account", "your key")27 if err != nil {28 panic(err)29 }30 instance, err := cloud.CreateInstance(&cloudapi.CreateInstanceOpts{31 })32 if err != nil {33 panic(err)34 }35 err = cloud.Retry(10, time.Second, func() (bool, error) {36 instance, err = cloud.GetInstance(instance.Id)37 if err != nil {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!