|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of vpc security_group_limits |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_vpc_security_group_limits" "security_group_limits" {} |
| 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 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 18 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 19 | +) |
| 20 | + |
| 21 | +func dataSourceTencentCloudVpcSecurityGroupLimits() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + Read: dataSourceTencentCloudVpcSecurityGroupLimitsRead, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "security_group_limit_set": { |
| 26 | + Computed: true, |
| 27 | + Type: schema.TypeList, |
| 28 | + Description: "sg limit set.", |
| 29 | + Elem: &schema.Resource{ |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "security_group_limit": { |
| 32 | + Type: schema.TypeInt, |
| 33 | + Computed: true, |
| 34 | + Description: "number of sg can be created.", |
| 35 | + }, |
| 36 | + "security_group_policy_limit": { |
| 37 | + Type: schema.TypeInt, |
| 38 | + Computed: true, |
| 39 | + Description: "number of sg polciy can be created.", |
| 40 | + }, |
| 41 | + "referred_security_group_limit": { |
| 42 | + Type: schema.TypeInt, |
| 43 | + Computed: true, |
| 44 | + Description: "number of sg can be referred.", |
| 45 | + }, |
| 46 | + "security_group_instance_limit": { |
| 47 | + Type: schema.TypeInt, |
| 48 | + Computed: true, |
| 49 | + Description: "number of sg associated instances.", |
| 50 | + }, |
| 51 | + "instance_security_group_limit": { |
| 52 | + Type: schema.TypeInt, |
| 53 | + Computed: true, |
| 54 | + Description: "number of instances associated sg.", |
| 55 | + }, |
| 56 | + "security_group_extended_policy_limit": { |
| 57 | + Type: schema.TypeInt, |
| 58 | + Computed: true, |
| 59 | + Description: "number of sg extended policy.", |
| 60 | + }, |
| 61 | + "security_group_referred_cvm_and_eni_limit": { |
| 62 | + Type: schema.TypeInt, |
| 63 | + Computed: true, |
| 64 | + Description: "number of eni and cvm can be referred.", |
| 65 | + }, |
| 66 | + "security_group_referred_svc_limit": { |
| 67 | + Type: schema.TypeInt, |
| 68 | + Computed: true, |
| 69 | + Description: "number of svc can be referred.", |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + |
| 75 | + "result_output_file": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Optional: true, |
| 78 | + Description: "Used to save results.", |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func dataSourceTencentCloudVpcSecurityGroupLimitsRead(d *schema.ResourceData, meta interface{}) error { |
| 85 | + defer logElapsed("data_source.tencentcloud_vpc_security_group_limits.read")() |
| 86 | + defer inconsistentCheck(d, meta)() |
| 87 | + |
| 88 | + logId := getLogId(contextNil) |
| 89 | + |
| 90 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 91 | + |
| 92 | + paramMap := make(map[string]interface{}) |
| 93 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 94 | + |
| 95 | + var securityGroupLimitSet *vpc.SecurityGroupLimitSet |
| 96 | + |
| 97 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 98 | + result, e := service.DescribeVpcSecurityGroupLimits(ctx, paramMap) |
| 99 | + if e != nil { |
| 100 | + return retryError(e) |
| 101 | + } |
| 102 | + securityGroupLimitSet = result |
| 103 | + return nil |
| 104 | + }) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + ids := make([]string, 0) |
| 110 | + securityGroupLimitSetMap := map[string]interface{}{} |
| 111 | + |
| 112 | + if securityGroupLimitSet != nil { |
| 113 | + if securityGroupLimitSet.SecurityGroupLimit != nil { |
| 114 | + securityGroupLimitSetMap["security_group_limit"] = securityGroupLimitSet.SecurityGroupLimit |
| 115 | + } |
| 116 | + |
| 117 | + if securityGroupLimitSet.SecurityGroupPolicyLimit != nil { |
| 118 | + securityGroupLimitSetMap["security_group_policy_limit"] = securityGroupLimitSet.SecurityGroupPolicyLimit |
| 119 | + } |
| 120 | + |
| 121 | + if securityGroupLimitSet.ReferedSecurityGroupLimit != nil { |
| 122 | + securityGroupLimitSetMap["referred_security_group_limit"] = securityGroupLimitSet.ReferedSecurityGroupLimit |
| 123 | + } |
| 124 | + |
| 125 | + if securityGroupLimitSet.SecurityGroupInstanceLimit != nil { |
| 126 | + securityGroupLimitSetMap["security_group_instance_limit"] = securityGroupLimitSet.SecurityGroupInstanceLimit |
| 127 | + } |
| 128 | + |
| 129 | + if securityGroupLimitSet.InstanceSecurityGroupLimit != nil { |
| 130 | + securityGroupLimitSetMap["instance_security_group_limit"] = securityGroupLimitSet.InstanceSecurityGroupLimit |
| 131 | + } |
| 132 | + |
| 133 | + if securityGroupLimitSet.SecurityGroupExtendedPolicyLimit != nil { |
| 134 | + securityGroupLimitSetMap["security_group_extended_policy_limit"] = securityGroupLimitSet.SecurityGroupExtendedPolicyLimit |
| 135 | + } |
| 136 | + |
| 137 | + if securityGroupLimitSet.SecurityGroupReferedCvmAndEniLimit != nil { |
| 138 | + securityGroupLimitSetMap["security_group_referred_cvm_and_eni_limit"] = securityGroupLimitSet.SecurityGroupReferedCvmAndEniLimit |
| 139 | + } |
| 140 | + |
| 141 | + if securityGroupLimitSet.SecurityGroupReferedSvcLimit != nil { |
| 142 | + securityGroupLimitSetMap["security_group_referred_svc_limit"] = securityGroupLimitSet.SecurityGroupReferedSvcLimit |
| 143 | + } |
| 144 | + |
| 145 | + ids = append(ids, helper.UInt64ToStr(*securityGroupLimitSet.SecurityGroupLimit)) |
| 146 | + _ = d.Set("security_group_limit_set", []interface{}{securityGroupLimitSetMap}) |
| 147 | + } |
| 148 | + |
| 149 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 150 | + output, ok := d.GetOk("result_output_file") |
| 151 | + if ok && output.(string) != "" { |
| 152 | + if e := writeToFile(output.(string), securityGroupLimitSetMap); e != nil { |
| 153 | + return e |
| 154 | + } |
| 155 | + } |
| 156 | + return nil |
| 157 | +} |
0 commit comments