|
| 1 | +/* |
| 2 | +Use this data source to query detailed acl information of Ckafka |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_ckafka_acls" "foo" { |
| 8 | + instance_id = "ckafka-f9ife4zz" |
| 9 | + resource_type = "TOPIC" |
| 10 | + resource_name = "topic-tf-test" |
| 11 | + host = "2" |
| 12 | +} |
| 13 | +``` |
| 14 | +*/ |
| 15 | +package tencentcloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 22 | + "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func dataSourceTencentCloudCkafkaAcls() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: dataSourceTencentCloudCkafkaAclsRead, |
| 28 | + |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "instance_id": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + Description: "Id of the ckafka instance.", |
| 34 | + }, |
| 35 | + "resource_type": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + Description: "ACL resource type. Valid values are `UNKNOWN`, `ANY`, `TOPIC`, `GROUP`, `CLUSTER`, `TRANSACTIONAL_ID`. Currently, only `TOPIC` is available, and other fields will be used for future ACLs compatible with open-source Kafka.", |
| 39 | + }, |
| 40 | + "resource_name": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + Description: "ACL resource name, which is related to `resource_type`. For example, if `resource_type` is `TOPIC`, this field indicates the topic name; if `resource_type` is `GROUP`, this field indicates the group name.", |
| 44 | + }, |
| 45 | + "host": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Optional: true, |
| 48 | + Description: "Host substr used for querying.", |
| 49 | + }, |
| 50 | + "result_output_file": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + Description: "Used to save results.", |
| 54 | + }, |
| 55 | + "acl_list": { |
| 56 | + Type: schema.TypeList, |
| 57 | + Computed: true, |
| 58 | + Description: "A list of ckafka acls. Each element contains the following attributes:", |
| 59 | + Elem: &schema.Resource{ |
| 60 | + Schema: map[string]*schema.Schema{ |
| 61 | + "resource_type": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: "ACL resource type.", |
| 65 | + }, |
| 66 | + "resource_name": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "ACL resource name, which is related to `resource_type`.", |
| 70 | + }, |
| 71 | + "operation_type": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "ACL operation mode.", |
| 75 | + }, |
| 76 | + "permission_type": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "ACL permission type, valid values are `UNKNOWN`, `ANY`, `DENY`, `ALLOW`, and `ALLOW` by default. Currently, CKafka supports `ALLOW` (equivalent to allow list), and other fields will be used for future ACLs compatible with open-source Kafka.", |
| 80 | + }, |
| 81 | + "host": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Computed: true, |
| 84 | + Description: "IP address allowed to access.", |
| 85 | + }, |
| 86 | + "principal": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + Description: "User which can access. `*` means that any user can access.", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func dataSourceTencentCloudCkafkaAclsRead(d *schema.ResourceData, meta interface{}) error { |
| 99 | + defer logElapsed("data_source.tencentcloud_ckafka_acls.read")() |
| 100 | + |
| 101 | + logId := getLogId(contextNil) |
| 102 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 103 | + |
| 104 | + params := make(map[string]interface{}) |
| 105 | + params["instance_id"] = d.Get("instance_id").(string) |
| 106 | + params["resource_type"] = d.Get("resource_type").(string) |
| 107 | + params["resource_name"] = d.Get("resource_name").(string) |
| 108 | + if v, ok := d.GetOk("host"); ok { |
| 109 | + params["host"] = v.(string) |
| 110 | + } |
| 111 | + |
| 112 | + ckafkaService := CkafkaService{ |
| 113 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 114 | + } |
| 115 | + aclInfos, err := ckafkaService.DescribeAclByFilter(ctx, params) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + aclList := make([]map[string]interface{}, 0, len(aclInfos)) |
| 120 | + ids := make([]string, 0, len(aclInfos)) |
| 121 | + for _, acl := range aclInfos { |
| 122 | + aclList = append(aclList, map[string]interface{}{ |
| 123 | + "resource_type": CKAFKA_ACL_RESOURCE_TYPE_TO_STRING[*acl.ResourceType], |
| 124 | + "resource_name": *acl.ResourceName, |
| 125 | + "operation_type": CKAFKA_ACL_OPERATION_TO_STRING[*acl.Operation], |
| 126 | + "permission_type": CKAFKA_PERMISSION_TYPE_TO_STRING[*acl.PermissionType], |
| 127 | + "host": *acl.Host, |
| 128 | + "principal": strings.TrimLeft(*acl.Principal, CKAFKA_ACL_PRINCIPAL_STR), |
| 129 | + }) |
| 130 | + |
| 131 | + ids = append(ids, params["instance_id"].(string)+FILED_SP+CKAFKA_PERMISSION_TYPE_TO_STRING[*acl.PermissionType]+ |
| 132 | + FILED_SP+strings.TrimLeft(*acl.Principal, CKAFKA_ACL_PRINCIPAL_STR)+FILED_SP+*acl.Host+FILED_SP+ |
| 133 | + CKAFKA_ACL_OPERATION_TO_STRING[*acl.Operation]+FILED_SP+CKAFKA_ACL_RESOURCE_TYPE_TO_STRING[*acl.ResourceType]+ |
| 134 | + FILED_SP+*acl.ResourceName) |
| 135 | + } |
| 136 | + |
| 137 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 138 | + d.Set("acl_list", aclList) |
| 139 | + |
| 140 | + output, ok := d.GetOk("result_output_file") |
| 141 | + if ok && output.(string) != "" { |
| 142 | + if e := writeToFile(output.(string), aclList); e != nil { |
| 143 | + return e |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
0 commit comments