Skip to content

Commit 996c55e

Browse files
authored
Merge pull request #11 from port-labs/relations-inside-bp
fixed relations to be a part of the bp schema
2 parents 0fa91b1 + ab0eeb0 commit 996c55e

File tree

3 files changed

+29
-190
lines changed

3 files changed

+29
-190
lines changed

port/cli/models.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ type (
4747

4848
Blueprint struct {
4949
Meta
50-
Identifier string `json:"identifier,omitempty"`
51-
Title string `json:"title"`
52-
Icon string `json:"icon"`
53-
Schema BlueprintSchema `json:"schema"`
50+
Identifier string `json:"identifier,omitempty"`
51+
Title string `json:"title"`
52+
Icon string `json:"icon"`
53+
Schema BlueprintSchema `json:"schema"`
54+
Relations map[string]Relation `json:"relations"`
5455
}
5556

5657
Action struct {

port/cli/relation.go

Lines changed: 0 additions & 73 deletions
This file was deleted.

port/resource_port_blueprint.go

Lines changed: 24 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -150,30 +150,8 @@ func readBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) d
150150
return diag.FromErr(err)
151151
}
152152
writeBlueprintFieldsToResource(d, b)
153-
relations, err := c.ReadRelations(ctx, d.Id())
154-
if err != nil {
155-
return diag.FromErr(err)
156-
}
157-
writeBlueprintRelationsToResource(d, relations)
158-
return diags
159-
}
160153

161-
func writeBlueprintRelationsToResource(d *schema.ResourceData, relations []*cli.Relation) {
162-
rels := schema.Set{F: func(i interface{}) int {
163-
id := (i.(map[string]interface{}))["identifier"].(string)
164-
return schema.HashString(id)
165-
}}
166-
for _, v := range relations {
167-
r := map[string]interface{}{
168-
"identifier": v.Identifier,
169-
"title": v.Title,
170-
"target": v.Target,
171-
"required": v.Required,
172-
"many": v.Many,
173-
}
174-
rels.Add(r)
175-
}
176-
d.Set("relations", &rels)
154+
return diags
177155
}
178156

179157
func writeBlueprintFieldsToResource(d *schema.ResourceData, b *cli.Blueprint) {
@@ -237,96 +215,40 @@ func blueprintResourceToBody(d *schema.ResourceData) (*cli.Blueprint, error) {
237215
properties[p["identifier"].(string)] = propFields
238216
}
239217

240-
b.Schema = cli.BlueprintSchema{Properties: properties}
241-
return b, nil
242-
}
243-
244-
func deleteBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
245-
var diags diag.Diagnostics
246-
c := m.(*cli.PortClient)
247-
err := c.DeleteBlueprint(ctx, d.Id())
248-
if err != nil {
249-
return diag.FromErr(err)
250-
}
251-
return diags
252-
}
253-
254-
func getRelations(d *schema.ResourceData) (rel []*cli.Relation) {
255-
relations, ok := d.GetOk("relations")
256-
if !ok {
257-
return nil
258-
}
259-
for _, relation := range relations.(*schema.Set).List() {
260-
relation := relation.(map[string]interface{})
261-
r := &cli.Relation{}
262-
if t, ok := relation["title"]; ok {
263-
r.Title = t.(string)
264-
}
265-
if t, ok := relation["target"]; ok {
266-
r.Target = t.(string)
218+
rels := d.Get("relations").(*schema.Set)
219+
relations := make(map[string]cli.Relation, props.Len())
220+
for _, rel := range rels.List() {
221+
p := rel.(map[string]interface{})
222+
relationFields := cli.Relation{}
223+
if t, ok := p["required"]; ok && t != "" {
224+
relationFields.Required = t.(bool)
267225
}
268-
if i, ok := relation["identifier"]; ok {
269-
r.Identifier = i.(string)
226+
if t, ok := p["many"]; ok && t != "" {
227+
relationFields.Many = t.(bool)
270228
}
271-
if req, ok := relation["required"]; ok {
272-
r.Required = req.(bool)
229+
if d, ok := p["title"]; ok && d != "" {
230+
relationFields.Title = d.(string)
273231
}
274-
if m, ok := relation["many"]; ok {
275-
r.Many = m.(bool)
232+
if d, ok := p["target"]; ok && d != "" {
233+
relationFields.Target = d.(string)
276234
}
277-
rel = append(rel, r)
278-
}
279-
return
280-
}
281235

282-
func createRelations(ctx context.Context, d *schema.ResourceData, m interface{}) error {
283-
c := m.(*cli.PortClient)
284-
rels := getRelations(d)
285-
for _, r := range rels {
286-
_, err := c.CreateRelation(ctx, d.Id(), r)
287-
if err != nil {
288-
return err
289-
}
236+
relations[p["identifier"].(string)] = relationFields
290237
}
291-
return nil
292-
}
293238

294-
func contains(s []string, e string) bool {
295-
for _, a := range s {
296-
if a == e {
297-
return true
298-
}
299-
}
300-
return false
239+
b.Schema = cli.BlueprintSchema{Properties: properties}
240+
b.Relations = relations
241+
return b, nil
301242
}
302243

303-
// patchDeleteDeprecatedRelations deletes relations that are no longer present in the resource.
304-
// This is necessary because we bundled relations inside the blueprint resource.
305-
// In the future, the API of blueprints should support getting the relations and then we can delete this patch.
306-
func patchDeleteDeprecatedRelations(ctx context.Context, d *schema.ResourceData, m interface{}) error {
244+
func deleteBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
245+
var diags diag.Diagnostics
307246
c := m.(*cli.PortClient)
308-
rels := getRelations(d)
309-
ids := make([]string, len(rels))
310-
for i, r := range rels {
311-
ids[i] = r.Identifier
312-
}
313-
remoteRelations, err := c.ReadRelations(ctx, d.Id())
247+
err := c.DeleteBlueprint(ctx, d.Id())
314248
if err != nil {
315-
return err
316-
}
317-
toDel := make([]*cli.Relation, 0)
318-
for _, r := range remoteRelations {
319-
if !contains(ids, r.Identifier) {
320-
toDel = append(toDel, r)
321-
}
322-
}
323-
for _, r := range toDel {
324-
err := c.DeleteRelation(ctx, d.Id(), r.Identifier)
325-
if err != nil {
326-
return err
327-
}
249+
return diag.FromErr(err)
328250
}
329-
return nil
251+
return diags
330252
}
331253

332254
func createBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
@@ -346,10 +268,7 @@ func createBlueprint(ctx context.Context, d *schema.ResourceData, m interface{})
346268
return diag.FromErr(err)
347269
}
348270
writeBlueprintComputedFieldsToResource(d, bp)
349-
err = createRelations(ctx, d, m)
350-
if err != nil {
351-
return diag.FromErr(err)
352-
}
271+
353272
return diags
354273
}
355274

@@ -370,14 +289,6 @@ func updateBlueprint(ctx context.Context, d *schema.ResourceData, m interface{})
370289
return diag.FromErr(err)
371290
}
372291
writeBlueprintComputedFieldsToResource(d, bp)
373-
err = patchDeleteDeprecatedRelations(ctx, d, m)
374-
if err != nil {
375-
return diag.FromErr(err)
376-
}
377-
err = createRelations(ctx, d, m)
378-
if err != nil {
379-
return diag.FromErr(err)
380-
}
381292
return diags
382293
}
383294

0 commit comments

Comments
 (0)