Skip to content

Commit

Permalink
Merge pull request #11 from port-labs/relations-inside-bp
Browse files Browse the repository at this point in the history
fixed relations to be a part of the bp schema
  • Loading branch information
danielsinai authored Sep 8, 2022
2 parents 0fa91b1 + ab0eeb0 commit 996c55e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 190 deletions.
9 changes: 5 additions & 4 deletions port/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ type (

Blueprint struct {
Meta
Identifier string `json:"identifier,omitempty"`
Title string `json:"title"`
Icon string `json:"icon"`
Schema BlueprintSchema `json:"schema"`
Identifier string `json:"identifier,omitempty"`
Title string `json:"title"`
Icon string `json:"icon"`
Schema BlueprintSchema `json:"schema"`
Relations map[string]Relation `json:"relations"`
}

Action struct {
Expand Down
73 changes: 0 additions & 73 deletions port/cli/relation.go

This file was deleted.

137 changes: 24 additions & 113 deletions port/resource_port_blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,8 @@ func readBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) d
return diag.FromErr(err)
}
writeBlueprintFieldsToResource(d, b)
relations, err := c.ReadRelations(ctx, d.Id())
if err != nil {
return diag.FromErr(err)
}
writeBlueprintRelationsToResource(d, relations)
return diags
}

func writeBlueprintRelationsToResource(d *schema.ResourceData, relations []*cli.Relation) {
rels := schema.Set{F: func(i interface{}) int {
id := (i.(map[string]interface{}))["identifier"].(string)
return schema.HashString(id)
}}
for _, v := range relations {
r := map[string]interface{}{
"identifier": v.Identifier,
"title": v.Title,
"target": v.Target,
"required": v.Required,
"many": v.Many,
}
rels.Add(r)
}
d.Set("relations", &rels)
return diags
}

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

b.Schema = cli.BlueprintSchema{Properties: properties}
return b, nil
}

func deleteBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*cli.PortClient)
err := c.DeleteBlueprint(ctx, d.Id())
if err != nil {
return diag.FromErr(err)
}
return diags
}

func getRelations(d *schema.ResourceData) (rel []*cli.Relation) {
relations, ok := d.GetOk("relations")
if !ok {
return nil
}
for _, relation := range relations.(*schema.Set).List() {
relation := relation.(map[string]interface{})
r := &cli.Relation{}
if t, ok := relation["title"]; ok {
r.Title = t.(string)
}
if t, ok := relation["target"]; ok {
r.Target = t.(string)
rels := d.Get("relations").(*schema.Set)
relations := make(map[string]cli.Relation, props.Len())
for _, rel := range rels.List() {
p := rel.(map[string]interface{})
relationFields := cli.Relation{}
if t, ok := p["required"]; ok && t != "" {
relationFields.Required = t.(bool)
}
if i, ok := relation["identifier"]; ok {
r.Identifier = i.(string)
if t, ok := p["many"]; ok && t != "" {
relationFields.Many = t.(bool)
}
if req, ok := relation["required"]; ok {
r.Required = req.(bool)
if d, ok := p["title"]; ok && d != "" {
relationFields.Title = d.(string)
}
if m, ok := relation["many"]; ok {
r.Many = m.(bool)
if d, ok := p["target"]; ok && d != "" {
relationFields.Target = d.(string)
}
rel = append(rel, r)
}
return
}

func createRelations(ctx context.Context, d *schema.ResourceData, m interface{}) error {
c := m.(*cli.PortClient)
rels := getRelations(d)
for _, r := range rels {
_, err := c.CreateRelation(ctx, d.Id(), r)
if err != nil {
return err
}
relations[p["identifier"].(string)] = relationFields
}
return nil
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
b.Schema = cli.BlueprintSchema{Properties: properties}
b.Relations = relations
return b, nil
}

// patchDeleteDeprecatedRelations deletes relations that are no longer present in the resource.
// This is necessary because we bundled relations inside the blueprint resource.
// In the future, the API of blueprints should support getting the relations and then we can delete this patch.
func patchDeleteDeprecatedRelations(ctx context.Context, d *schema.ResourceData, m interface{}) error {
func deleteBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*cli.PortClient)
rels := getRelations(d)
ids := make([]string, len(rels))
for i, r := range rels {
ids[i] = r.Identifier
}
remoteRelations, err := c.ReadRelations(ctx, d.Id())
err := c.DeleteBlueprint(ctx, d.Id())
if err != nil {
return err
}
toDel := make([]*cli.Relation, 0)
for _, r := range remoteRelations {
if !contains(ids, r.Identifier) {
toDel = append(toDel, r)
}
}
for _, r := range toDel {
err := c.DeleteRelation(ctx, d.Id(), r.Identifier)
if err != nil {
return err
}
return diag.FromErr(err)
}
return nil
return diags
}

func createBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -346,10 +268,7 @@ func createBlueprint(ctx context.Context, d *schema.ResourceData, m interface{})
return diag.FromErr(err)
}
writeBlueprintComputedFieldsToResource(d, bp)
err = createRelations(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}

return diags
}

Expand All @@ -370,14 +289,6 @@ func updateBlueprint(ctx context.Context, d *schema.ResourceData, m interface{})
return diag.FromErr(err)
}
writeBlueprintComputedFieldsToResource(d, bp)
err = patchDeleteDeprecatedRelations(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
err = createRelations(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
return diags
}

Expand Down

0 comments on commit 996c55e

Please sign in to comment.