@@ -7,6 +7,7 @@ package provisioner
77
88import (
99 "context"
10+ "strings"
1011
1112 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -15,6 +16,8 @@ import (
1516 "github.com/vmware/terraform-provider-tanzu-mission-control/internal/authctx"
1617 clienterrors "github.com/vmware/terraform-provider-tanzu-mission-control/internal/client/errors"
1718 "github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper"
19+ objectmetamodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/objectmeta"
20+ provisioner "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/provisioner"
1821 "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common"
1922)
2023
@@ -28,35 +31,48 @@ func DataSourceProvisioner() *schema.Resource {
2831}
2932
3033var provisionerListSchema = map [string ]* schema.Schema {
31- nameKey : {
32- Type : schema .TypeString ,
33- Description : "Name of the provisioner" ,
34- Optional : true ,
35- },
36- managementClusterNameKey : {
37- Type : schema .TypeString ,
38- Description : "Name of the management cluster" ,
39- Required : true ,
40- ForceNew : true ,
41- },
42- orgIDKey : {
43- Type : schema .TypeString ,
44- Description : "ID of the organization" ,
34+ provisionerKey : {
35+ Type : schema .TypeList ,
36+ Description : "Provisioners info" ,
4537 Optional : true ,
38+ Elem : & schema.Resource {
39+ Schema : map [string ]* schema.Schema {
40+ nameKey : {
41+ Type : schema .TypeString ,
42+ Description : "Name of the provisioner" ,
43+ Optional : true ,
44+ },
45+ managementClusterNameKey : {
46+ Type : schema .TypeString ,
47+ Description : "Name of the management cluster" ,
48+ Required : true ,
49+ ForceNew : true ,
50+ },
51+ orgIDKey : {
52+ Type : schema .TypeString ,
53+ Description : "ID of the organization" ,
54+ Optional : true ,
55+ },
56+ common .MetaKey : common .Meta ,
57+ },
58+ },
4659 },
47- common .MetaKey : common .Meta ,
4860}
4961
5062func dataSourceProvisionerRead (ctx context.Context , d * schema.ResourceData , m interface {}) (diags diag.Diagnostics ) {
5163 config := m .(authctx.TanzuContext )
5264
53- model , err := tfModelConverter .ConvertTFSchemaToAPIModel (d , []string {nameKey , managementClusterNameKey })
54- if err != nil {
65+ id := make ([]string , 0 )
66+
67+ var resp * provisioner.VmwareTanzuManageV1alpha1ManagementclusterProvisionerListprovisionersResponse
68+
69+ model , err := tfModelDataConverter .ConvertTFSchemaToAPIModel (d , []string {provisionerKey , nameKey , managementClusterNameKey })
70+ if err != nil || model == nil || model .Provisioners == nil {
5571 return diag .FromErr (errors .Wrapf (err , "Couldn't read Tanzu Mission Control provisioner configurations." ))
5672 }
5773
58- if model .FullName .Name == "" {
59- resp , err : = config .TMCConnection .ProvisionerResourceService .ProvisionerResourceServiceList (model .FullName )
74+ if model .Provisioners [ 0 ]. FullName .Name == "" {
75+ resp , err = config .TMCConnection .ProvisionerResourceService .ProvisionerResourceServiceList (model . Provisioners [ 0 ] .FullName )
6076 if err != nil {
6177 if clienterrors .IsNotFoundError (err ) && ! helper .IsDataRead (ctx ) {
6278 _ = schema .RemoveFromState (d , m )
@@ -65,16 +81,8 @@ func dataSourceProvisionerRead(ctx context.Context, d *schema.ResourceData, m in
6581
6682 return
6783 }
68-
69- for i := range resp .Provisioners {
70- d .SetId (resp .Provisioners [i ].Meta .UID )
71-
72- if err := d .Set (common .MetaKey , common .FlattenMeta (resp .Provisioners [i ].Meta )); err != nil {
73- return diag .FromErr (err )
74- }
75- }
7684 } else {
77- resp , err := config .TMCConnection .ProvisionerResourceService .ProvisionerResourceServiceGet (model .FullName )
85+ getResp , err := config .TMCConnection .ProvisionerResourceService .ProvisionerResourceServiceGet (model . Provisioners [ 0 ] .FullName )
7886 if err != nil {
7987 if clienterrors .IsNotFoundError (err ) && ! helper .IsDataRead (ctx ) {
8088 _ = schema .RemoveFromState (d , m )
@@ -83,12 +91,38 @@ func dataSourceProvisionerRead(ctx context.Context, d *schema.ResourceData, m in
8391 return
8492 }
8593
86- d .SetId (resp .Provisioner .Meta .UID )
87-
88- if err := d .Set (common .MetaKey , common .FlattenMeta (resp .Provisioner .Meta )); err != nil {
89- return diag .FromErr (err )
94+ p := & provisioner.VmwareTanzuManageV1alpha1ManagementclusterProvisionerListprovisionersResponse {
95+ Provisioners : []* provisioner.VmwareTanzuManageV1alpha1ManagementclusterProvisionerProvisioner {
96+ {
97+ FullName : & provisioner.VmwareTanzuManageV1alpha1ManagementclusterProvisionerFullName {
98+ ManagementClusterName : getResp .Provisioner .FullName .ManagementClusterName ,
99+ Name : getResp .Provisioner .FullName .Name ,
100+ OrgID : getResp .Provisioner .FullName .OrgID ,
101+ },
102+ Meta : & objectmetamodel.VmwareTanzuCoreV1alpha1ObjectMeta {
103+ Description : getResp .Provisioner .Meta .Description ,
104+ Labels : getResp .Provisioner .Meta .Labels ,
105+ UID : getResp .Provisioner .Meta .UID ,
106+ ResourceVersion : getResp .Provisioner .Meta .ResourceVersion ,
107+ },
108+ },
109+ },
90110 }
111+
112+ resp = p
113+ }
114+
115+ err = tfModelDataConverter .FillTFSchema (resp , d )
116+
117+ if err != nil {
118+ return diag .FromErr (errors .Wrapf (err , "Unable to populate tf schema" ))
91119 }
92120
121+ for _ , prov := range resp .Provisioners {
122+ id = append (id , prov .Meta .UID )
123+ }
124+
125+ d .SetId (strings .Join (id , "_" ))
126+
93127 return diags
94128}
0 commit comments