|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of ssm products |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_ssm_products" "products" {} |
| 8 | +``` |
| 9 | +*/ |
| 10 | +package tencentcloud |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 17 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 18 | +) |
| 19 | + |
| 20 | +func dataSourceTencentCloudSsmProducts() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + Read: dataSourceTencentCloudSsmProductsRead, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "products": { |
| 25 | + Computed: true, |
| 26 | + Type: schema.TypeSet, |
| 27 | + Elem: &schema.Schema{ |
| 28 | + Type: schema.TypeString, |
| 29 | + }, |
| 30 | + Description: "List of supported services.", |
| 31 | + }, |
| 32 | + |
| 33 | + "result_output_file": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + Description: "Used to save results.", |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func dataSourceTencentCloudSsmProductsRead(d *schema.ResourceData, meta interface{}) error { |
| 43 | + defer logElapsed("data_source.tencentcloud_ssm_products.read")() |
| 44 | + defer inconsistentCheck(d, meta)() |
| 45 | + |
| 46 | + logId := getLogId(contextNil) |
| 47 | + |
| 48 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 49 | + |
| 50 | + service := SsmService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 51 | + |
| 52 | + var products []*string |
| 53 | + |
| 54 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 55 | + result, e := service.DescribeSsmProductsByFilter(ctx) |
| 56 | + if e != nil { |
| 57 | + return retryError(e) |
| 58 | + } |
| 59 | + products = result |
| 60 | + return nil |
| 61 | + }) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + if products != nil { |
| 67 | + _ = d.Set("products", products) |
| 68 | + } |
| 69 | + ids := helper.StrListToStr(products) |
| 70 | + |
| 71 | + d.SetId(ids) |
| 72 | + output, ok := d.GetOk("result_output_file") |
| 73 | + if ok && output.(string) != "" { |
| 74 | + if e := writeToFile(output.(string), products); e != nil { |
| 75 | + return e |
| 76 | + } |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
0 commit comments