@@ -95,50 +95,64 @@ func dataSourceCloudAccountNSXT() *schema.Resource {
9595func dataSourceCloudAccountNSXTRead (d * schema.ResourceData , meta interface {}) error {
9696 apiClient := meta .(* Client ).apiClient
9797
98- id , idOk := d .GetOk ("id" )
99- name , nameOk := d .GetOk ("name" )
98+ id := d .Get ("id" ).( string )
99+ name := d .Get ("name" ).( string )
100100
101- if ! idOk && ! nameOk {
102- return fmt .Errorf ("one of 'id' or 'name' must be assigned " )
101+ if id == "" && name == "" {
102+ return fmt .Errorf ("one of 'id' or 'name' must be set " )
103103 }
104104
105- getResp , err := apiClient .CloudAccount .GetNsxTCloudAccounts (cloud_account .NewGetNsxTCloudAccountsParams ())
106- if err != nil {
107- return err
108- }
109-
110- setFields := func (account * models.CloudAccountNsxT ) error {
111- d .SetId (* account .ID )
112- d .Set ("associated_cloud_account_ids" , flattenAssociatedCloudAccountIDs (account .Links ))
113- d .Set ("created_at" , account .CreatedAt )
114- d .Set ("dc_id" , account .Dcid )
115- d .Set ("description" , account .Description )
116- d .Set ("hostname" , account .HostName )
117- d .Set ("manager_mode" , account .ManagerMode )
118- d .Set ("name" , account .Name )
119- d .Set ("org_id" , account .OrgID )
120- d .Set ("owner" , account .Owner )
121- d .Set ("updated_at" , account .UpdatedAt )
122- d .Set ("username" , account .Username )
123-
124- if err := d .Set ("links" , flattenLinks (account .Links )); err != nil {
125- return fmt .Errorf ("error setting cloud_account_nsxt links - error: %#v" , err )
105+ var cloudAccountNsxT * models.CloudAccountNsxT
106+ if id != "" {
107+ getResp , err := apiClient .CloudAccount .GetNsxTCloudAccount (cloud_account .NewGetNsxTCloudAccountParams ().WithID (id ))
108+ if err != nil {
109+ switch err .(type ) {
110+ case * cloud_account.GetNsxTCloudAccountNotFound :
111+ return fmt .Errorf ("nsxt cloud account with id '%s' not found" , id )
112+ default :
113+ // nop
114+ }
115+ return err
126116 }
127117
128- if err := d .Set ("tags" , flattenTags (account .Tags )); err != nil {
129- return fmt .Errorf ("error setting cloud_account_nsxt tags - error: %#v" , err )
118+ cloudAccountNsxT = getResp .GetPayload ()
119+ } else {
120+ getResp , err := apiClient .CloudAccount .GetNsxTCloudAccounts (cloud_account .NewGetNsxTCloudAccountsParams ())
121+ if err != nil {
122+ return err
130123 }
131124
132- return nil
133- }
134- for _ , account := range getResp .Payload .Content {
135- if idOk && account .ID == id {
136- return setFields (account )
125+ for _ , account := range getResp .Payload .Content {
126+ if account .Name == name {
127+ cloudAccountNsxT = account
128+ }
137129 }
138- if nameOk && account .Name == name {
139- return setFields (account )
130+
131+ if cloudAccountNsxT == nil {
132+ return fmt .Errorf ("nsxt cloud account with name '%s' not found" , name )
140133 }
141134 }
142135
143- return fmt .Errorf ("cloud account %s not found" , name )
136+ d .SetId (* cloudAccountNsxT .ID )
137+ d .Set ("associated_cloud_account_ids" , flattenAssociatedCloudAccountIDs (cloudAccountNsxT .Links ))
138+ d .Set ("created_at" , cloudAccountNsxT .CreatedAt )
139+ d .Set ("dc_id" , cloudAccountNsxT .Dcid )
140+ d .Set ("description" , cloudAccountNsxT .Description )
141+ d .Set ("hostname" , cloudAccountNsxT .HostName )
142+ d .Set ("manager_mode" , cloudAccountNsxT .ManagerMode )
143+ d .Set ("name" , cloudAccountNsxT .Name )
144+ d .Set ("org_id" , cloudAccountNsxT .OrgID )
145+ d .Set ("owner" , cloudAccountNsxT .Owner )
146+ d .Set ("updated_at" , cloudAccountNsxT .UpdatedAt )
147+ d .Set ("username" , cloudAccountNsxT .Username )
148+
149+ if err := d .Set ("links" , flattenLinks (cloudAccountNsxT .Links )); err != nil {
150+ return fmt .Errorf ("error setting cloud_account_nsxt links - error: %#v" , err )
151+ }
152+
153+ if err := d .Set ("tags" , flattenTags (cloudAccountNsxT .Tags )); err != nil {
154+ return fmt .Errorf ("error setting cloud_account_nsxt tags - error: %#v" , err )
155+ }
156+
157+ return nil
144158}
0 commit comments