|
| 1 | +package tencentcloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 11 | +) |
| 12 | + |
| 13 | +// go test -i; go test -test.run TestAccTencentCloudTsfMicroserviceResource_basic -v |
| 14 | +func TestAccTencentCloudTsfMicroserviceResource_basic(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + resource.Test(t, resource.TestCase{ |
| 18 | + PreCheck: func() { |
| 19 | + testAccPreCheck(t) |
| 20 | + }, |
| 21 | + Providers: testAccProviders, |
| 22 | + CheckDestroy: testAccCheckTsfMicroserviceDestroy, |
| 23 | + Steps: []resource.TestStep{ |
| 24 | + { |
| 25 | + Config: testAccTsfMicroservice, |
| 26 | + Check: resource.ComposeTestCheckFunc( |
| 27 | + testAccCheckTsfMicroserviceExists("tencentcloud_tsf_microservice.microservice"), |
| 28 | + resource.TestCheckResourceAttrSet("tencentcloud_tsf_microservice.microservice", "id"), |
| 29 | + resource.TestCheckResourceAttr("tencentcloud_tsf_microservice.microservice", "microservice_name", "test-microservice"), |
| 30 | + resource.TestCheckResourceAttr("tencentcloud_tsf_microservice.microservice", "microservice_desc", "desc-microservice"), |
| 31 | + resource.TestCheckResourceAttr("tencentcloud_tsf_microservice.microservice", "tags.createdBy", "terraform"), |
| 32 | + ), |
| 33 | + }, |
| 34 | + { |
| 35 | + ResourceName: "tencentcloud_tsf_microservice.microservice", |
| 36 | + ImportState: true, |
| 37 | + ImportStateVerify: true, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func testAccCheckTsfMicroserviceDestroy(s *terraform.State) error { |
| 44 | + logId := getLogId(contextNil) |
| 45 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 46 | + service := TsfService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn} |
| 47 | + for _, rs := range s.RootModule().Resources { |
| 48 | + if rs.Type != "tencentcloud_tsf_microservice" { |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + idSplit := strings.Split(rs.Primary.ID, FILED_SP) |
| 53 | + if len(idSplit) != 2 { |
| 54 | + return fmt.Errorf("id is broken,%s", rs.Primary.ID) |
| 55 | + } |
| 56 | + namespaceId := idSplit[0] |
| 57 | + microserviceId := idSplit[1] |
| 58 | + |
| 59 | + res, err := service.DescribeTsfMicroserviceById(ctx, namespaceId, microserviceId, "") |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + if res != nil { |
| 65 | + return fmt.Errorf("tsf microservice %s still exists", rs.Primary.ID) |
| 66 | + } |
| 67 | + } |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +func testAccCheckTsfMicroserviceExists(r string) resource.TestCheckFunc { |
| 72 | + return func(s *terraform.State) error { |
| 73 | + logId := getLogId(contextNil) |
| 74 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 75 | + |
| 76 | + rs, ok := s.RootModule().Resources[r] |
| 77 | + if !ok { |
| 78 | + return fmt.Errorf("resource %s is not found", r) |
| 79 | + } |
| 80 | + idSplit := strings.Split(rs.Primary.ID, FILED_SP) |
| 81 | + if len(idSplit) != 2 { |
| 82 | + return fmt.Errorf("id is broken,%s", rs.Primary.ID) |
| 83 | + } |
| 84 | + namespaceId := idSplit[0] |
| 85 | + microserviceId := idSplit[1] |
| 86 | + |
| 87 | + service := TsfService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn} |
| 88 | + res, err := service.DescribeTsfMicroserviceById(ctx, namespaceId, microserviceId, "") |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + if res == nil { |
| 94 | + return fmt.Errorf("tsf microservice %s is not found", rs.Primary.ID) |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +const testAccTsfMicroserviceVar = ` |
| 102 | +variable "namespace_id" { |
| 103 | + default = "` + defaultNamespaceId + `" |
| 104 | +} |
| 105 | +` |
| 106 | + |
| 107 | +const testAccTsfMicroservice = testAccTsfMicroserviceVar + ` |
| 108 | +
|
| 109 | +resource "tencentcloud_tsf_microservice" "microservice" { |
| 110 | + namespace_id = var.namespace_id |
| 111 | + microservice_name = "test-microservice" |
| 112 | + microservice_desc = "desc-microservice" |
| 113 | + tags = { |
| 114 | + "createdBy" = "terraform" |
| 115 | + } |
| 116 | +} |
| 117 | +
|
| 118 | +` |
0 commit comments