Skip to content

Commit

Permalink
Merge branch 'master' into PORT-test-dirien-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
dvirsegev committed Jul 20, 2023
2 parents 2e91088 + e653f89 commit 9d850ae
Show file tree
Hide file tree
Showing 15 changed files with 835 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@talsabagport @danielsinai @dvirsegev
@talsabagport @danielsinai @dvirsegev @matarpeles
47 changes: 46 additions & 1 deletion internal/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,51 @@ type (
EnumColors map[string]string `json:"enumColors,omitempty"`
}

ActionProperty struct {
Type string `json:"type,omitempty"`
Title *string `json:"title,omitempty"`
Identifier string `json:"identifier,omitempty"`
Items map[string]any `json:"items,omitempty"`
Default interface{} `json:"default,omitempty"`
Icon *string `json:"icon,omitempty"`
Format *string `json:"format,omitempty"`
MaxLength *int `json:"maxLength,omitempty"`
MinLength *int `json:"minLength,omitempty"`
MaxItems *int `json:"maxItems,omitempty"`
MinItems *int `json:"minItems,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Description *string `json:"description,omitempty"`
Blueprint *string `json:"blueprint,omitempty"`
Pattern *string `json:"pattern,omitempty"`
Enum interface{} `json:"enum,omitempty"`
Spec *string `json:"spec,omitempty"`
SpecAuthentication *SpecAuthentication `json:"specAuthentication,omitempty"`
EnumColors map[string]string `json:"enumColors,omitempty"`
DependsOn []string `json:"dependsOn,omitempty"`
Dataset *Dataset `json:"dataset,omitempty"`
}

SpecAuthentication struct {
ClientId string `json:"clientId,omitempty"`
AuthorizationUrl string `json:"authorizationUrl,omitempty"`
TokenUrl string `json:"tokenUrl,omitempty"`
}

DatasetValue struct {
JqQuery string `json:"jqQuery,omitempty"`
}
DatasetRule struct {
Blueprint *string `json:"blueprint,omitempty"`
Property *string `json:"property,omitempty"`
Operator string `json:"operator,omitempty"`
Value *DatasetValue `json:"value,omitempty"`
}
Dataset struct {
Combinator string `json:"combinator,omitempty"`
Rules []DatasetRule `json:"rules,omitempty"`
}

BlueprintCalculationProperty struct {
Type string `json:"type,omitempty"`
Title *string `json:"title,omitempty"`
Expand Down Expand Up @@ -95,6 +134,9 @@ type (
OmitUserInputs *bool `json:"omitUserInputs,omitempty"`
ReportWorkflowStatus *bool `json:"reportWorkflowStatus,omitempty"`
Branch *string `json:"branch,omitempty"`
ProjectName *string `json:"projectName,omitempty"`
GroupName *string `json:"groupName,omitempty"`
DefaultRef *string `json:"defaultRef,omitempty"`
}

ApprovalNotification struct {
Expand All @@ -113,7 +155,10 @@ type (
Path string `json:"path,omitempty"`
}

ActionUserInputs = BlueprintSchema
ActionUserInputs = struct {
Properties map[string]ActionProperty `json:"properties"`
Required []string `json:"required,omitempty"`
}

Blueprint struct {
Meta
Expand Down
1 change: 1 addition & 0 deletions internal/consts/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ const (
Kafka = "KAFKA"
AzureDevops = "AZURE-DEVOPS"
Github = "GITHUB"
Gitlab = "GITLAB"
)
16 changes: 16 additions & 0 deletions internal/flex/string.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package flex

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

func GoStringToFramework(v *string) types.String {
Expand All @@ -11,3 +15,15 @@ func GoStringToFramework(v *string) types.String {

return types.StringValue(*v)
}

func GoArrayStringToTerraformList(ctx context.Context, array []string) types.List {
if array == nil {
return types.ListNull(types.StringType)
}
attrs := make([]attr.Value, 0, len(array))
for _, value := range array {
attrs = append(attrs, basetypes.NewStringValue(value))
}
list, _ := types.ListValue(types.StringType, attrs)
return list
}
10 changes: 10 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,13 @@ func GoObjectToTerraformString(v interface{}) (types.String, error) {
value := string(js)
return types.StringValue(value), nil
}

func InterfaceToStringArray(o interface{}) []string {
items := o.([]interface{})
res := make([]string, len(items))
for i, item := range items {
res[i] = item.(string)
}

return res
}
68 changes: 64 additions & 4 deletions port/action/actionStateToPortBody.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ import (
"github.com/port-labs/terraform-provider-port-labs/internal/consts"
)

func actionDataSetToPortBody(dataSet *DatasetModel) *cli.Dataset {
cliDateSet := &cli.Dataset{
Combinator: dataSet.Combinator.ValueString(),
}
rules := make([]cli.DatasetRule, 0, len(dataSet.Rules))
for _, rule := range dataSet.Rules {
dataSetRule := cli.DatasetRule{
Operator: rule.Operator.ValueString(),
Value: &cli.DatasetValue{
JqQuery: rule.Value.JqQuery.ValueString(),
},
}
if !rule.Blueprint.IsNull() {
blueprint := rule.Blueprint.ValueString()
dataSetRule.Blueprint = &blueprint
}
if !rule.Property.IsNull() {
rule := rule.Property.ValueString()
dataSetRule.Property = &rule
}

rules = append(rules, dataSetRule)
}
cliDateSet.Rules = rules
return cliDateSet
}

func actionStateToPortBody(ctx context.Context, data *ActionModel, bp *cli.Blueprint) (*cli.Action, error) {
action := &cli.Action{
Identifier: data.Identifier.ValueString(),
Expand Down Expand Up @@ -55,15 +82,15 @@ func actionStateToPortBody(ctx context.Context, data *ActionModel, bp *cli.Bluep
return nil, err
}
} else {
action.UserInputs.Properties = make(map[string]cli.BlueprintProperty)
action.UserInputs.Properties = make(map[string]cli.ActionProperty)
}

return action, nil
}

func actionPropertiesToBody(ctx context.Context, action *cli.Action, data *ActionModel) error {
required := []string{}
props := map[string]cli.BlueprintProperty{}
props := map[string]cli.ActionProperty{}
var err error
if data.UserProperties.StringProps != nil {
err = stringPropResourceToBody(ctx, data, props, &required)
Expand All @@ -75,11 +102,11 @@ func actionPropertiesToBody(ctx context.Context, action *cli.Action, data *Actio
err = numberPropResourceToBody(ctx, data, props, &required)
}
if data.UserProperties.BooleanProps != nil {
booleanPropResourceToBody(data, props, &required)
err = booleanPropResourceToBody(ctx, data, props, &required)
}

if data.UserProperties.ObjectProps != nil {
err = objectPropResourceToBody(data, props, &required)
err = objectPropResourceToBody(ctx, data, props, &required)
}

if err != nil {
Expand Down Expand Up @@ -148,5 +175,38 @@ func invocationMethodToBody(data *ActionModel) *cli.InvocationMethod {
}
return webhookInvocation
}

if data.GitlabMethod != nil {
projectName := data.GitlabMethod.ProjectName.ValueString()
groupName := data.GitlabMethod.GroupName.ValueString()
gitlabInvocation := &cli.InvocationMethod{
Type: consts.Gitlab,
ProjectName: &projectName,
GroupName: &groupName,
}

if !data.GitlabMethod.OmitPayload.IsNull() {
omitPayload := data.GitlabMethod.OmitPayload.ValueBool()
gitlabInvocation.OmitPayload = &omitPayload
}

if !data.GitlabMethod.OmitUserInputs.IsNull() {
omitUserInputs := data.GitlabMethod.OmitUserInputs.ValueBool()
gitlabInvocation.OmitUserInputs = &omitUserInputs
}

if !data.GitlabMethod.DefaultRef.IsNull() {
defaultRef := data.GitlabMethod.DefaultRef.ValueString()
gitlabInvocation.DefaultRef = &defaultRef
}

if !data.GitlabMethod.Agent.IsNull() {
agent := data.GitlabMethod.Agent.ValueBool()
gitlabInvocation.Agent = &agent
}

return gitlabInvocation
}

return nil
}
36 changes: 32 additions & 4 deletions port/action/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/port-labs/terraform-provider-port-labs/internal/utils"
)

func handleArrayItemsToBody(ctx context.Context, property *cli.BlueprintProperty, prop ArrayPropModel, required *[]string) error {
func handleArrayItemsToBody(ctx context.Context, property *cli.ActionProperty, prop ArrayPropModel, required *[]string) error {
if prop.StringItems != nil {
items := map[string]interface{}{}
items["type"] = "string"
Expand Down Expand Up @@ -86,9 +86,9 @@ func handleArrayItemsToBody(ctx context.Context, property *cli.BlueprintProperty
return nil
}

func arrayPropResourceToBody(ctx context.Context, d *ActionModel, props map[string]cli.BlueprintProperty, required *[]string) error {
func arrayPropResourceToBody(ctx context.Context, d *ActionModel, props map[string]cli.ActionProperty, required *[]string) error {
for propIdentifier, prop := range d.UserProperties.ArrayProps {
props[propIdentifier] = cli.BlueprintProperty{
props[propIdentifier] = cli.ActionProperty{
Type: "array",
}

Expand All @@ -104,6 +104,14 @@ func arrayPropResourceToBody(ctx context.Context, d *ActionModel, props map[stri
property.Icon = &icon
}

if !prop.DefaultJqQuery.IsNull() {
defaultJqQuery := prop.DefaultJqQuery.ValueString()
jqQueryMap := map[string]string{
"jqQuery": defaultJqQuery,
}
property.Default = jqQueryMap
}

if !prop.Description.IsNull() {
description := prop.Description.ValueString()
property.Description = &description
Expand All @@ -118,6 +126,18 @@ func arrayPropResourceToBody(ctx context.Context, d *ActionModel, props map[stri
property.MaxItems = &maxItems
}

if !prop.DependsOn.IsNull() {
dependsOn, err := utils.TerraformListToGoArray(ctx, prop.DependsOn, "string")
if err != nil {
return err
}
property.DependsOn = utils.InterfaceToStringArray(dependsOn)

}
if prop.Dataset != nil {
property.Dataset = actionDataSetToPortBody(prop.Dataset)
}

err := handleArrayItemsToBody(ctx, &property, prop, required)
if err != nil {
return err
Expand All @@ -133,12 +153,20 @@ func arrayPropResourceToBody(ctx context.Context, d *ActionModel, props map[stri
return nil
}

func addArrayPropertiesToResource(v *cli.BlueprintProperty) (*ArrayPropModel, error) {
func addArrayPropertiesToResource(v *cli.ActionProperty) (*ArrayPropModel, error) {
arrayProp := &ArrayPropModel{
MinItems: flex.GoInt64ToFramework(v.MinItems),
MaxItems: flex.GoInt64ToFramework(v.MaxItems),
}

if v.Default != nil {
switch v := v.Default.(type) {
// We only test for map[string]interface{} ATM
case map[string]interface{}:
arrayProp.DefaultJqQuery = types.StringValue(v["jqQuery"].(string))
}
}

if v.Items != nil {
if v.Items["type"] != "" {
switch v.Items["type"] {
Expand Down
32 changes: 29 additions & 3 deletions port/action/boolean.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package action

import "github.com/port-labs/terraform-provider-port-labs/internal/cli"
import (
"context"

func booleanPropResourceToBody(d *ActionModel, props map[string]cli.BlueprintProperty, required *[]string) {
"github.com/port-labs/terraform-provider-port-labs/internal/cli"
"github.com/port-labs/terraform-provider-port-labs/internal/utils"
)

func booleanPropResourceToBody(ctx context.Context, d *ActionModel, props map[string]cli.ActionProperty, required *[]string) error {
for propIdentifier, prop := range d.UserProperties.BooleanProps {
props[propIdentifier] = cli.BlueprintProperty{
props[propIdentifier] = cli.ActionProperty{
Type: "boolean",
}

Expand All @@ -18,6 +23,14 @@ func booleanPropResourceToBody(d *ActionModel, props map[string]cli.BlueprintPro
property.Default = prop.Default.ValueBool()
}

if !prop.DefaultJqQuery.IsNull() {
defaultJqQuery := prop.DefaultJqQuery.ValueString()
jqQueryMap := map[string]string{
"jqQuery": defaultJqQuery,
}
property.Default = jqQueryMap
}

if !prop.Icon.IsNull() {
icon := prop.Icon.ValueString()
property.Icon = &icon
Expand All @@ -28,10 +41,23 @@ func booleanPropResourceToBody(d *ActionModel, props map[string]cli.BlueprintPro
property.Description = &description
}

if !prop.DependsOn.IsNull() {
dependsOn, err := utils.TerraformListToGoArray(ctx, prop.DependsOn, "string")
if err != nil {
return err
}
property.DependsOn = utils.InterfaceToStringArray(dependsOn)

}
if prop.Dataset != nil {
property.Dataset = actionDataSetToPortBody(prop.Dataset)
}

props[propIdentifier] = property
}
if prop.Required.ValueBool() {
*required = append(*required, propIdentifier)
}
}
return nil
}
Loading

0 comments on commit 9d850ae

Please sign in to comment.