Skip to content

Commit ae07183

Browse files
committed
Fix vra_blueprint datasource lookup by id
* Update attributes * Fix lookup logic by id Signed-off-by: Ferran Rodenas <[email protected]>
1 parent dcd4cc5 commit ae07183

File tree

1 file changed

+145
-118
lines changed

1 file changed

+145
-118
lines changed

vra/data_source_blueprint.go

Lines changed: 145 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"fmt"
55

66
"github.com/go-openapi/strfmt"
7+
78
"github.com/vmware/vra-sdk-go/pkg/client/blueprint"
9+
"github.com/vmware/vra-sdk-go/pkg/models"
810

911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10-
"github.com/vmware/vra-sdk-go/pkg/models"
1112
)
1213

1314
func dataSourceBlueprint() *schema.Resource {
@@ -16,130 +17,160 @@ func dataSourceBlueprint() *schema.Resource {
1617

1718
Schema: map[string]*schema.Schema{
1819
"content": {
19-
Type: schema.TypeString,
20-
Computed: true,
20+
Type: schema.TypeString,
21+
Computed: true,
22+
Description: "Cloud template YAML content.",
2123
},
2224
"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.",
2528
},
2629
"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.",
2933
},
3034
"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.",
3338
},
3439
"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.",
3743
Elem: &schema.Schema{
3844
Type: schema.TypeString,
3945
},
4046
},
4147
"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.",
4451
},
4552
"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.",
4856
},
4957
"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.",
5261
},
5362
"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.",
5666
},
5767
"description": {
58-
Type: schema.TypeString,
59-
Computed: true,
68+
Type: schema.TypeString,
69+
Computed: true,
70+
Description: "A human-friendly description.",
6071
},
6172
"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.",
6578
},
6679
"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.",
6985
},
7086
"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.",
7390
},
7491
"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.",
7896
},
7997
"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.",
82101
},
83102
"request_scope_org": {
84103
Type: schema.TypeBool,
85104
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.",
87106
},
88107
"self_link": {
89-
Type: schema.TypeString,
90-
Computed: true,
108+
Type: schema.TypeString,
109+
Computed: true,
110+
Description: "HATEOAS of the entity.",
91111
},
92112
"status": {
93-
Type: schema.TypeString,
94-
Computed: true,
113+
Type: schema.TypeString,
114+
Computed: true,
115+
Description: "Status of the cloud template.",
95116
},
96117
"total_released_versions": {
97-
Type: schema.TypeInt,
98-
Computed: true,
118+
Type: schema.TypeInt,
119+
Computed: true,
120+
Description: "Total number of released versions.",
99121
},
100122
"total_versions": {
101-
Type: schema.TypeInt,
102-
Computed: true,
123+
Type: schema.TypeInt,
124+
Computed: true,
125+
Description: "Total number of versions.",
103126
},
104127
"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.",
107131
},
108132
"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.",
111136
},
112137
"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.",
115141
},
116142
"validation_messages": {
117143
Type: schema.TypeSet,
118144
Computed: true,
119145
Elem: &schema.Resource{
120146
Schema: map[string]*schema.Schema{
121147
"message": {
122-
Type: schema.TypeString,
123-
Computed: true,
148+
Type: schema.TypeString,
149+
Computed: true,
150+
Description: "Validation message.",
124151
},
125152
"metadata": {
126-
Type: schema.TypeMap,
127-
Optional: true,
153+
Type: schema.TypeMap,
154+
Computed: true,
155+
Description: "Validation metadata.",
128156
Elem: &schema.Schema{
129157
Type: schema.TypeString,
130158
},
131159
},
132160
"path": {
133-
Type: schema.TypeString,
134-
Computed: true,
161+
Type: schema.TypeString,
162+
Computed: true,
163+
Description: "Validation path.",
135164
},
136165
"resource_name": {
137-
Type: schema.TypeString,
138-
Computed: true,
166+
Type: schema.TypeString,
167+
Computed: true,
168+
Description: "Name of the resource.",
139169
},
140170
"type": {
141-
Type: schema.TypeString,
142-
Computed: true,
171+
Type: schema.TypeString,
172+
Computed: true,
173+
Description: "Message type.",
143174
},
144175
},
145176
},
@@ -153,75 +184,71 @@ func dataSourceBlueprintRead(d *schema.ResourceData, meta interface{}) error {
153184

154185
id, idOk := d.GetOk("id")
155186
name, nameOk := d.GetOk("name")
156-
projectID, projectIDOk := d.GetOk("project_id")
157187

158188
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")
160190
}
161191

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+
}
182202

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+
}
186210

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
211214
}
212215

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
223223
}
224+
return err
224225
}
225226

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
227254
}

0 commit comments

Comments
 (0)