|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cvm chc_denied_actions |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cvm_chc_denied_actions" "chc_denied_actions" { |
| 8 | + chc_ids = ["chc-xxxxx"] |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 19 | + cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" |
| 20 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 21 | +) |
| 22 | + |
| 23 | +func dataSourceTencentCloudCvmChcDeniedActions() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Read: dataSourceTencentCloudCvmChcDeniedActionsRead, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "chc_ids": { |
| 28 | + Required: true, |
| 29 | + Type: schema.TypeSet, |
| 30 | + Elem: &schema.Schema{ |
| 31 | + Type: schema.TypeString, |
| 32 | + }, |
| 33 | + Description: "CHC host IDs.", |
| 34 | + }, |
| 35 | + |
| 36 | + "chc_host_denied_action_set": { |
| 37 | + Computed: true, |
| 38 | + Type: schema.TypeList, |
| 39 | + Description: "Actions not allowed for the CHC instance.", |
| 40 | + Elem: &schema.Resource{ |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "chc_id": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + Description: "CHC instance ID.", |
| 46 | + }, |
| 47 | + "state": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "CHC instance status.", |
| 51 | + }, |
| 52 | + "deny_actions": { |
| 53 | + Type: schema.TypeSet, |
| 54 | + Elem: &schema.Schema{ |
| 55 | + Type: schema.TypeString, |
| 56 | + }, |
| 57 | + Computed: true, |
| 58 | + Description: "Actions not allowed for the current CHC instance.", |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + |
| 64 | + "result_output_file": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Optional: true, |
| 67 | + Description: "Used to save results.", |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func dataSourceTencentCloudCvmChcDeniedActionsRead(d *schema.ResourceData, meta interface{}) error { |
| 74 | + defer logElapsed("data_source.tencentcloud_cvm_chc_denied_actions.read")() |
| 75 | + defer inconsistentCheck(d, meta)() |
| 76 | + |
| 77 | + logId := getLogId(contextNil) |
| 78 | + |
| 79 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 80 | + |
| 81 | + paramMap := make(map[string]interface{}) |
| 82 | + if v, ok := d.GetOk("chc_ids"); ok { |
| 83 | + chcIdsSet := v.(*schema.Set).List() |
| 84 | + paramMap["chc_ids"] = helper.InterfacesStrings(chcIdsSet) |
| 85 | + } |
| 86 | + |
| 87 | + service := CvmService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 88 | + |
| 89 | + var chcHostDeniedActionSet []*cvm.ChcHostDeniedActions |
| 90 | + |
| 91 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 92 | + result, e := service.DescribeCvmChcDeniedActionsByFilter(ctx, paramMap) |
| 93 | + if e != nil { |
| 94 | + return retryError(e) |
| 95 | + } |
| 96 | + chcHostDeniedActionSet = result |
| 97 | + return nil |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + ids := make([]string, 0, len(chcHostDeniedActionSet)) |
| 104 | + tmpList := make([]map[string]interface{}, 0, len(chcHostDeniedActionSet)) |
| 105 | + |
| 106 | + if len(chcHostDeniedActionSet) > 0 { |
| 107 | + for _, chcHostDeniedActions := range chcHostDeniedActionSet { |
| 108 | + chcHostDeniedActionsMap := map[string]interface{}{} |
| 109 | + |
| 110 | + if chcHostDeniedActions.ChcId != nil { |
| 111 | + chcHostDeniedActionsMap["chc_id"] = chcHostDeniedActions.ChcId |
| 112 | + } |
| 113 | + |
| 114 | + if chcHostDeniedActions.State != nil { |
| 115 | + chcHostDeniedActionsMap["state"] = chcHostDeniedActions.State |
| 116 | + } |
| 117 | + |
| 118 | + if chcHostDeniedActions.DenyActions != nil { |
| 119 | + chcHostDeniedActionsMap["deny_actions"] = chcHostDeniedActions.DenyActions |
| 120 | + } |
| 121 | + |
| 122 | + ids = append(ids, *chcHostDeniedActions.ChcId) |
| 123 | + tmpList = append(tmpList, chcHostDeniedActionsMap) |
| 124 | + } |
| 125 | + |
| 126 | + _ = d.Set("chc_host_denied_action_set", tmpList) |
| 127 | + } |
| 128 | + |
| 129 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 130 | + output, ok := d.GetOk("result_output_file") |
| 131 | + if ok && output.(string) != "" { |
| 132 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 133 | + return e |
| 134 | + } |
| 135 | + } |
| 136 | + return nil |
| 137 | +} |
0 commit comments