|
| 1 | +// Generated by PMS #598 |
| 2 | +package vpc |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + |
| 7 | + "github.com/hashicorp/go-multierror" |
| 8 | + "github.com/hashicorp/go-uuid" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/tidwall/gjson" |
| 12 | + |
| 13 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 14 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper" |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" |
| 16 | +) |
| 17 | + |
| 18 | +func DataSourceVpcNetworkingSecgroupTags() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + ReadContext: dataSourceVpcNetworkingSecgroupTagsRead, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "region": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + Computed: true, |
| 27 | + Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`, |
| 28 | + }, |
| 29 | + "tags": { |
| 30 | + Type: schema.TypeList, |
| 31 | + Computed: true, |
| 32 | + Description: `The list of tags`, |
| 33 | + Elem: &schema.Resource{ |
| 34 | + Schema: map[string]*schema.Schema{ |
| 35 | + "key": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + Description: `The tag key.`, |
| 39 | + }, |
| 40 | + "values": { |
| 41 | + Type: schema.TypeList, |
| 42 | + Computed: true, |
| 43 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 44 | + Description: `The tag values.`, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +type NetworkingSecgroupTagsDSWrapper struct { |
| 54 | + *schemas.ResourceDataWrapper |
| 55 | + Config *config.Config |
| 56 | +} |
| 57 | + |
| 58 | +func newNetworkingSecgroupTagsDSWrapper(d *schema.ResourceData, meta interface{}) *NetworkingSecgroupTagsDSWrapper { |
| 59 | + return &NetworkingSecgroupTagsDSWrapper{ |
| 60 | + ResourceDataWrapper: schemas.NewSchemaWrapper(d), |
| 61 | + Config: meta.(*config.Config), |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func dataSourceVpcNetworkingSecgroupTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 66 | + wrapper := newNetworkingSecgroupTagsDSWrapper(d, meta) |
| 67 | + lisSecGroTagRst, err := wrapper.ListSecurityGroupTags() |
| 68 | + if err != nil { |
| 69 | + return diag.FromErr(err) |
| 70 | + } |
| 71 | + |
| 72 | + id, err := uuid.GenerateUUID() |
| 73 | + if err != nil { |
| 74 | + return diag.FromErr(err) |
| 75 | + } |
| 76 | + d.SetId(id) |
| 77 | + |
| 78 | + err = wrapper.listSecurityGroupTagsToSchema(lisSecGroTagRst) |
| 79 | + if err != nil { |
| 80 | + return diag.FromErr(err) |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// @API VPC GET /v2.0/{project_id}/security-groups/tags |
| 87 | +func (w *NetworkingSecgroupTagsDSWrapper) ListSecurityGroupTags() (*gjson.Result, error) { |
| 88 | + client, err := w.NewClient(w.Config, "vpc") |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + uri := "/v2.0/{project_id}/security-groups/tags" |
| 94 | + return httphelper.New(client). |
| 95 | + Method("GET"). |
| 96 | + URI(uri). |
| 97 | + Request(). |
| 98 | + Result() |
| 99 | +} |
| 100 | + |
| 101 | +func (w *NetworkingSecgroupTagsDSWrapper) listSecurityGroupTagsToSchema(body *gjson.Result) error { |
| 102 | + d := w.ResourceData |
| 103 | + mErr := multierror.Append(nil, |
| 104 | + d.Set("region", w.Config.GetRegion(w.ResourceData)), |
| 105 | + d.Set("tags", schemas.SliceToList(body.Get("tags"), |
| 106 | + func(tags gjson.Result) any { |
| 107 | + return map[string]any{ |
| 108 | + "key": tags.Get("key").Value(), |
| 109 | + "values": schemas.SliceToStrList(tags.Get("values")), |
| 110 | + } |
| 111 | + }, |
| 112 | + )), |
| 113 | + ) |
| 114 | + return mErr.ErrorOrNil() |
| 115 | +} |
0 commit comments