@@ -4,10 +4,11 @@ import (
4
4
"fmt"
5
5
6
6
"github.com/go-openapi/strfmt"
7
+
7
8
"github.com/vmware/vra-sdk-go/pkg/client/blueprint"
9
+ "github.com/vmware/vra-sdk-go/pkg/models"
8
10
9
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10
- "github.com/vmware/vra-sdk-go/pkg/models"
11
12
)
12
13
13
14
func dataSourceBlueprint () * schema.Resource {
@@ -16,130 +17,160 @@ func dataSourceBlueprint() *schema.Resource {
16
17
17
18
Schema : map [string ]* schema.Schema {
18
19
"content" : {
19
- Type : schema .TypeString ,
20
- Computed : true ,
20
+ Type : schema .TypeString ,
21
+ Computed : true ,
22
+ Description : "Cloud template YAML content." ,
21
23
},
22
24
"content_source_id" : {
23
- Type : schema .TypeString ,
24
- Computed : true ,
25
+ Type : schema .TypeString ,
26
+ Computed : true ,
27
+ Description : "The id of the content source." ,
25
28
},
26
29
"content_source_path" : {
27
- Type : schema .TypeString ,
28
- Computed : true ,
30
+ Type : schema .TypeString ,
31
+ Computed : true ,
32
+ Description : "The path of the content source." ,
29
33
},
30
34
"content_source_sync_at" : {
31
- Type : schema .TypeString ,
32
- Computed : true ,
35
+ Type : schema .TypeString ,
36
+ Computed : true ,
37
+ Description : "Content source last sync at." ,
33
38
},
34
39
"content_source_sync_messages" : {
35
- Type : schema .TypeSet ,
36
- Computed : true ,
40
+ Type : schema .TypeSet ,
41
+ Computed : true ,
42
+ Description : "Content source last sync status." ,
37
43
Elem : & schema.Schema {
38
44
Type : schema .TypeString ,
39
45
},
40
46
},
41
47
"content_source_sync_status" : {
42
- Type : schema .TypeString ,
43
- Computed : true ,
48
+ Type : schema .TypeString ,
49
+ Computed : true ,
50
+ Description : "Content source last sync status." ,
44
51
},
45
52
"content_source_type" : {
46
- Type : schema .TypeString ,
47
- Computed : true ,
53
+ Type : schema .TypeString ,
54
+ Computed : true ,
55
+ Description : "The type of the content source." ,
48
56
},
49
57
"created_at" : {
50
- Type : schema .TypeString ,
51
- Computed : true ,
58
+ Type : schema .TypeString ,
59
+ Computed : true ,
60
+ Description : "Date when the entity was created. The date is in ISO 6801 and UTC." ,
52
61
},
53
62
"created_by" : {
54
- Type : schema .TypeString ,
55
- Computed : true ,
63
+ Type : schema .TypeString ,
64
+ Computed : true ,
65
+ Description : "The user the entity was created by." ,
56
66
},
57
67
"description" : {
58
- Type : schema .TypeString ,
59
- Computed : true ,
68
+ Type : schema .TypeString ,
69
+ Computed : true ,
70
+ Description : "A human-friendly description." ,
60
71
},
61
72
"id" : {
62
- Type : schema .TypeString ,
63
- Optional : true ,
64
- Computed : true ,
73
+ Type : schema .TypeString ,
74
+ Optional : true ,
75
+ Computed : true ,
76
+ ConflictsWith : []string {"name" },
77
+ Description : "The id of the cloud template." ,
65
78
},
66
79
"name" : {
67
- Type : schema .TypeString ,
68
- Optional : true ,
80
+ Type : schema .TypeString ,
81
+ Optional : true ,
82
+ Computed : true ,
83
+ ConflictsWith : []string {"id" },
84
+ Description : "The name of the cloud template." ,
69
85
},
70
86
"org_id" : {
71
- Type : schema .TypeString ,
72
- Computed : true ,
87
+ Type : schema .TypeString ,
88
+ Computed : true ,
89
+ Description : "The id of the organization this entity belongs to." ,
73
90
},
74
91
"project_id" : {
75
- Type : schema .TypeString ,
76
- Optional : true ,
77
- Computed : true ,
92
+ Type : schema .TypeString ,
93
+ Optional : true ,
94
+ Computed : true ,
95
+ Description : "The id of the project to narrow the search while looking for cloud templates." ,
78
96
},
79
97
"project_name" : {
80
- Type : schema .TypeString ,
81
- Computed : true ,
98
+ Type : schema .TypeString ,
99
+ Computed : true ,
100
+ Description : "The name of the project the entity belongs to." ,
82
101
},
83
102
"request_scope_org" : {
84
103
Type : schema .TypeBool ,
85
104
Computed : true ,
86
- Description : "Flag to indicate blueprint can be requested from any project in org " ,
105
+ Description : "Flag to indicate whether this cloud template can be requested from any project in the organization this entity belongs to. " ,
87
106
},
88
107
"self_link" : {
89
- Type : schema .TypeString ,
90
- Computed : true ,
108
+ Type : schema .TypeString ,
109
+ Computed : true ,
110
+ Description : "HATEOAS of the entity." ,
91
111
},
92
112
"status" : {
93
- Type : schema .TypeString ,
94
- Computed : true ,
113
+ Type : schema .TypeString ,
114
+ Computed : true ,
115
+ Description : "Status of the cloud template." ,
95
116
},
96
117
"total_released_versions" : {
97
- Type : schema .TypeInt ,
98
- Computed : true ,
118
+ Type : schema .TypeInt ,
119
+ Computed : true ,
120
+ Description : "Total number of released versions." ,
99
121
},
100
122
"total_versions" : {
101
- Type : schema .TypeInt ,
102
- Computed : true ,
123
+ Type : schema .TypeInt ,
124
+ Computed : true ,
125
+ Description : "Total number of versions." ,
103
126
},
104
127
"updated_at" : {
105
- Type : schema .TypeString ,
106
- Computed : true ,
128
+ Type : schema .TypeString ,
129
+ Computed : true ,
130
+ Description : "Date when the entity was last updated. The date is ISO 8601 and UTC." ,
107
131
},
108
132
"updated_by" : {
109
- Type : schema .TypeString ,
110
- Computed : true ,
133
+ Type : schema .TypeString ,
134
+ Computed : true ,
135
+ Description : "The user the entity was last updated by." ,
111
136
},
112
137
"valid" : {
113
- Type : schema .TypeBool ,
114
- Computed : true ,
138
+ Type : schema .TypeBool ,
139
+ Computed : true ,
140
+ Description : "Flag to indicate if the current content of the cloud template is valid." ,
115
141
},
116
142
"validation_messages" : {
117
143
Type : schema .TypeSet ,
118
144
Computed : true ,
119
145
Elem : & schema.Resource {
120
146
Schema : map [string ]* schema.Schema {
121
147
"message" : {
122
- Type : schema .TypeString ,
123
- Computed : true ,
148
+ Type : schema .TypeString ,
149
+ Computed : true ,
150
+ Description : "Validation message." ,
124
151
},
125
152
"metadata" : {
126
- Type : schema .TypeMap ,
127
- Optional : true ,
153
+ Type : schema .TypeMap ,
154
+ Computed : true ,
155
+ Description : "Validation metadata." ,
128
156
Elem : & schema.Schema {
129
157
Type : schema .TypeString ,
130
158
},
131
159
},
132
160
"path" : {
133
- Type : schema .TypeString ,
134
- Computed : true ,
161
+ Type : schema .TypeString ,
162
+ Computed : true ,
163
+ Description : "Validation path." ,
135
164
},
136
165
"resource_name" : {
137
- Type : schema .TypeString ,
138
- Computed : true ,
166
+ Type : schema .TypeString ,
167
+ Computed : true ,
168
+ Description : "Name of the resource." ,
139
169
},
140
170
"type" : {
141
- Type : schema .TypeString ,
142
- Computed : true ,
171
+ Type : schema .TypeString ,
172
+ Computed : true ,
173
+ Description : "Message type." ,
143
174
},
144
175
},
145
176
},
@@ -153,75 +184,71 @@ func dataSourceBlueprintRead(d *schema.ResourceData, meta interface{}) error {
153
184
154
185
id , idOk := d .GetOk ("id" )
155
186
name , nameOk := d .GetOk ("name" )
156
- projectID , projectIDOk := d .GetOk ("project_id" )
157
187
158
188
if ! idOk && ! nameOk {
159
- return fmt .Errorf ("one of id or name must be assigned " )
189
+ return fmt .Errorf ("one of id or name is required " )
160
190
}
161
191
162
- var resp * blueprint.ListBlueprintsUsingGET1OK
163
- var err error
164
- projects := make ([]string , 1 )
165
-
166
- if projectIDOk {
167
- projects = append (projects , projectID .(string ))
168
- resp , err = apiClient .Blueprint .ListBlueprintsUsingGET1 (
169
- blueprint .NewListBlueprintsUsingGET1Params ().WithName (withString (name .(string ))).WithProjects (projects ))
170
- } else {
171
- resp , err = apiClient .Blueprint .ListBlueprintsUsingGET1 (
172
- blueprint .NewListBlueprintsUsingGET1Params ().WithName (withString (name .(string ))))
173
- }
174
-
175
- if err != nil {
176
- return err
177
- }
178
-
179
- if resp .GetPayload ().NumberOfElements == 0 {
180
- return fmt .Errorf ("blueprint %s not found" , name )
181
- }
192
+ var blueprintResource * models.Blueprint
193
+ if ! idOk {
194
+ listBlueprintsUsingGET1Params := blueprint .NewListBlueprintsUsingGET1Params ().WithName (withString (name .(string )))
195
+ if projectID , projectIDOk := d .GetOk ("project_id" ); projectIDOk {
196
+ listBlueprintsUsingGET1Params = listBlueprintsUsingGET1Params .WithProjects ([]string {projectID .(string )})
197
+ }
198
+ getResp , err := apiClient .Blueprint .ListBlueprintsUsingGET1 (listBlueprintsUsingGET1Params )
199
+ if err != nil {
200
+ return err
201
+ }
182
202
183
- if resp .GetPayload ().NumberOfElements > 1 {
184
- return fmt .Errorf ("more than one blueprint found with the same name, try to narrow filter by project_id" )
185
- }
203
+ blueprints := getResp .GetPayload ()
204
+ if len (blueprints .Content ) > 1 {
205
+ return fmt .Errorf ("more than one blueprint found with the same name, try to narrow filter by project_id" )
206
+ }
207
+ if len (blueprints .Content ) == 0 {
208
+ return fmt .Errorf ("blueprint %s not found" , name )
209
+ }
186
210
187
- setFields := func (blueprint * models.Blueprint ) {
188
- d .SetId (blueprint .ID )
189
- d .Set ("content" , blueprint .Content )
190
- d .Set ("content_source_id" , blueprint .ContentSourceID )
191
- d .Set ("content_source_pat" , blueprint .ContentSourcePath )
192
- d .Set ("content_source_sync_at" , blueprint .ContentSourceSyncAt )
193
- d .Set ("content_source_sync_messages" , blueprint .ContentSourceSyncMessages )
194
- d .Set ("content_source_sync_status" , blueprint .ContentSourceSyncStatus )
195
- d .Set ("content_source_type" , blueprint .ContentSourceType )
196
- d .Set ("created_at" , blueprint .CreatedAt )
197
- d .Set ("created_by" , blueprint .CreatedBy )
198
- d .Set ("description" , blueprint .Description )
199
- d .Set ("name" , blueprint .Name )
200
- d .Set ("org_id" , blueprint .OrgID )
201
- d .Set ("project_id" , blueprint .ProjectID )
202
- d .Set ("project_name" , blueprint .ProjectName )
203
- d .Set ("request_scope_org" , blueprint .RequestScopeOrg )
204
- d .Set ("self_link" , blueprint .SelfLink )
205
- d .Set ("status" , blueprint .Status )
206
- d .Set ("total_released_versions" , blueprint .TotalReleasedVersions )
207
- d .Set ("total_versions" , blueprint .TotalVersions )
208
- d .Set ("updated_at" , blueprint .UpdatedAt )
209
- d .Set ("updated_by" , blueprint .UpdatedBy )
210
- d .Set ("valid" , blueprint .Valid )
211
+ // ListBlueprintsUsingGET1Params does not return the blueprint content, so we need to use the GetBlueprintUsingGET1
212
+ // call in order to retrieve it.
213
+ id = blueprints .Content [0 ].ID
211
214
}
212
215
213
- for _ , bp := range resp .Payload .Content {
214
- if (idOk && bp .ID == id ) || (nameOk && bp .Name == name .(string )) {
215
- bpDetails , err := apiClient .Blueprint .GetBlueprintUsingGET1 (
216
- blueprint .NewGetBlueprintUsingGET1Params ().WithBlueprintID (strfmt .UUID (bp .ID )))
217
- if err != nil {
218
- return err
219
- }
220
- setFields (bpDetails .GetPayload ())
221
-
222
- return nil
216
+ getResp , err := apiClient .Blueprint .GetBlueprintUsingGET1 (blueprint .NewGetBlueprintUsingGET1Params ().WithBlueprintID (strfmt .UUID (id .(string ))))
217
+ if err != nil {
218
+ switch err .(type ) {
219
+ case * blueprint.GetBlueprintUsingGET1NotFound :
220
+ return fmt .Errorf ("blueprint '%s' not found" , id )
221
+ default :
222
+ // nop
223
223
}
224
+ return err
224
225
}
225
226
226
- return fmt .Errorf ("blueprint %s not found" , name )
227
+ blueprintResource = getResp .GetPayload ()
228
+
229
+ d .SetId (blueprintResource .ID )
230
+ d .Set ("content" , blueprintResource .Content )
231
+ d .Set ("content_source_id" , blueprintResource .ContentSourceID )
232
+ d .Set ("content_source_pat" , blueprintResource .ContentSourcePath )
233
+ d .Set ("content_source_sync_at" , blueprintResource .ContentSourceSyncAt )
234
+ d .Set ("content_source_sync_messages" , blueprintResource .ContentSourceSyncMessages )
235
+ d .Set ("content_source_sync_status" , blueprintResource .ContentSourceSyncStatus )
236
+ d .Set ("content_source_type" , blueprintResource .ContentSourceType )
237
+ d .Set ("created_at" , blueprintResource .CreatedAt )
238
+ d .Set ("created_by" , blueprintResource .CreatedBy )
239
+ d .Set ("description" , blueprintResource .Description )
240
+ d .Set ("name" , blueprintResource .Name )
241
+ d .Set ("org_id" , blueprintResource .OrgID )
242
+ d .Set ("project_id" , blueprintResource .ProjectID )
243
+ d .Set ("project_name" , blueprintResource .ProjectName )
244
+ d .Set ("request_scope_org" , blueprintResource .RequestScopeOrg )
245
+ d .Set ("self_link" , blueprintResource .SelfLink )
246
+ d .Set ("status" , blueprintResource .Status )
247
+ d .Set ("total_released_versions" , blueprintResource .TotalReleasedVersions )
248
+ d .Set ("total_versions" , blueprintResource .TotalVersions )
249
+ d .Set ("updated_at" , blueprintResource .UpdatedAt )
250
+ d .Set ("updated_by" , blueprintResource .UpdatedBy )
251
+ d .Set ("valid" , blueprintResource .Valid )
252
+
253
+ return nil
227
254
}
0 commit comments