Skip to content

Commit

Permalink
deprecate entity prop types (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedwigz authored Aug 22, 2022
1 parent 20f6331 commit ac5da81
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## 0.2.0
FEATURES:
- `resource/port-labs_entity`: The attribute `properties/type` is DEPRECATED. You do not need to specify the type of a property, and it is inferred from the blueprint. It is recommended to remove `type`.

BREAKING CHANGES:
- `resource/port-labs_blueprint`: Removed deprecated attribute `data_source`.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ HOSTNAME=github.com
NAMESPACE=port-labs
NAME=port-labs
BINARY=terraform-provider-${NAME}
VERSION=0.1.1
VERSION=0.2.0
OS=$(shell go env GOOS)
ARCH=$(shell go env GOARCH)
OS_ARCH=${OS}_${ARCH}
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/blueprint.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Port blueprint

### Optional

- `data_source` (String) The data source for entities of this blueprint
- `data_source` (String, Deprecated) The data source for entities of this blueprint
- `relations` (Block Set) The blueprints that are connected to this blueprint (see [below for nested schema](#nestedblock--relations))

### Read-Only
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ Port entity
Required:

- `name` (String) The name of this property
- `type` (String) The type of the property

Optional:

- `items` (List of String) The list of items, in case the type of this property is a list
- `type` (String, Deprecated) The type of the property
- `value` (String) The value for this property


Expand Down
1 change: 0 additions & 1 deletion examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ resource "port-labs_entity" "microservice" {
properties {
name = "microservice_name"
value = "golang_monolith"
type = "string"
}
}
1 change: 0 additions & 1 deletion examples/provider/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ resource "port-labs_entity" "microservice" {
properties {
name = "microservice_name"
value = "golang_monolith"
type = "string"
}
}
2 changes: 0 additions & 2 deletions examples/resources/port-labs_entity/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ resource "port-labs_entity" "microservice" {
properties {
name = "microservice_name"
value = "golang_monolith"
type = "string"
}
}

Expand All @@ -18,6 +17,5 @@ resource "port-labs_entity" "prod_env" {
properties {
name = "name"
value = "production-env"
type = "string"
}
}
1 change: 0 additions & 1 deletion port/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type (
Identifier string `json:"identifier,omitempty"`
Title string `json:"title"`
Icon string `json:"icon"`
DataSource string `json:"dataSource"`
Schema BlueprintSchema `json:"schema"`
// TODO: relations
}
Expand Down
5 changes: 1 addition & 4 deletions port/resource_port_blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func newBlueprintResource() *schema.Resource {
Description: "The data source for entities of this blueprint",
Default: "Port",
Optional: true,
Deprecated: "Data source is ignored",
},
"icon": {
Type: schema.TypeString,
Expand Down Expand Up @@ -172,7 +173,6 @@ func writeBlueprintFieldsToResource(d *schema.ResourceData, b *cli.Blueprint) {
d.SetId(b.Identifier)
d.Set("title", b.Title)
d.Set("icon", b.Icon)
d.Set("data_source", b.DataSource)
d.Set("created_at", b.CreatedAt.String())
d.Set("created_by", b.CreatedBy)
d.Set("updated_at", b.UpdatedAt.String())
Expand Down Expand Up @@ -206,9 +206,6 @@ func blueprintResourceToBody(d *schema.ResourceData) (*cli.Blueprint, error) {

b.Title = d.Get("title").(string)
b.Icon = d.Get("icon").(string)
if ds, ok := d.GetOk("data_source"); ok {
b.DataSource = ds.(string)
}

props := d.Get("properties").(*schema.Set)
properties := make(map[string]cli.BlueprintProperty, props.Len())
Expand Down
28 changes: 17 additions & 11 deletions port/resource_port_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func newEntityResource() *schema.Resource {
"type": {
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"number", "string", "boolean", "array", "object"}, false),
Required: true,
Optional: true,
Deprecated: "property type is not required anymore",
Description: "The type of the property",
},
"value": {
Expand Down Expand Up @@ -117,8 +118,15 @@ func deleteEntity(ctx context.Context, d *schema.ResourceData, m interface{}) di
return diags
}

func convert(prop map[string]interface{}) (interface{}, error) {
func convert(prop map[string]interface{}, bp *cli.Blueprint) (interface{}, error) {
valType := prop["type"].(string)
if valType == "" {
if p, ok := bp.Schema.Properties[prop["name"].(string)]; ok {
valType = p.Type
} else {
return nil, fmt.Errorf("no type found for property %s", prop["name"])
}
}
switch valType {
case "string", "number", "boolean":
return prop["value"], nil
Expand All @@ -135,7 +143,7 @@ func convert(prop map[string]interface{}) (interface{}, error) {
return "", fmt.Errorf("unsupported type %s", valType)
}

func entityResourceToBody(d *schema.ResourceData) (*cli.Entity, error) {
func entityResourceToBody(d *schema.ResourceData, bp *cli.Blueprint) (*cli.Entity, error) {
e := &cli.Entity{}
if identifier, ok := d.GetOk("identifier"); ok {
e.Identifier = identifier.(string)
Expand All @@ -157,7 +165,7 @@ func entityResourceToBody(d *schema.ResourceData) (*cli.Entity, error) {
properties := make(map[string]interface{}, props.Len())
for _, prop := range props.List() {
p := prop.(map[string]interface{})
propValue, err := convert(p)
propValue, err := convert(p, bp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -194,23 +202,17 @@ func writeEntityFieldsToResource(d *schema.ResourceData, e *cli.Entity) {
p["name"] = k
switch t := v.(type) {
case map[string]interface{}:
p["type"] = "object"
js, _ := json.Marshal(&t)
p["value"] = string(js)
case []interface{}:
p["type"] = "array"
p["items"] = t
case float64:
p["type"] = "number"
p["value"] = strconv.FormatFloat(t, 'f', -1, 64)
case int:
p["type"] = "number"
p["value"] = strconv.Itoa(t)
case string:
p["type"] = "string"
p["value"] = t
case bool:
p["type"] = "boolean"
p["value"] = "false"
if t {
p["value"] = "true"
Expand All @@ -224,7 +226,11 @@ func writeEntityFieldsToResource(d *schema.ResourceData, e *cli.Entity) {
func createEntity(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*cli.PortClient)
e, err := entityResourceToBody(d)
bp, err := c.ReadBlueprint(ctx, d.Get("blueprint").(string))
if err != nil {
return diag.FromErr(err)
}
e, err := entityResourceToBody(d, bp)
if err != nil {
return diag.FromErr(err)
}
Expand Down
9 changes: 0 additions & 9 deletions port/resource_port_entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func TestAccPortEntityUpdateProp(t *testing.T) {
properties {
name = "text"
value = "hedwig"
type = "string"
}
}
`
Expand All @@ -28,7 +27,6 @@ func TestAccPortEntityUpdateProp(t *testing.T) {
properties {
name = "text"
value = "hedwig2"
type = "string"
}
}
`
Expand Down Expand Up @@ -57,27 +55,22 @@ func TestAccPortEntity(t *testing.T) {
properties {
name = "text"
value = "hedwig"
type = "string"
}
properties {
name = "bool"
value = "true"
type = "boolean"
}
properties {
name = "num"
value = 123
type = "number"
}
properties {
name = "arr"
items = [1,2,3]
type = "array"
}
properties {
name = "obj"
value = jsonencode({"a":"b"})
type = "object"
}
}
`
Expand Down Expand Up @@ -106,7 +99,6 @@ func TestAccPortEntitiesRelation(t *testing.T) {
properties {
name = "text"
value = "test-relation"
type = "string"
}
}
resource "port-labs_entity" "microservice2" {
Expand All @@ -115,7 +107,6 @@ func TestAccPortEntitiesRelation(t *testing.T) {
properties {
name = "str"
value = "test-relation"
type = "string"
}
}
`
Expand Down

0 comments on commit ac5da81

Please sign in to comment.