Skip to content

Commit

Permalink
Mark some parameters as computed in terraform provider via spec (#125)
Browse files Browse the repository at this point in the history
Terraform has a concept of optional attributes, where optional attributes can be left null, and will use default values.
Another concept is computed attributes - attributes that are set by the provider and cannot be changed by the user.
Finally, optional attributes must be marked as computed.

This change adds a way to mark some parameters in the specification as computed (e.g. uuid parameters) and properly marks attributes with default values as optional+computed.
  • Loading branch information
kklimonda-cl authored Aug 8, 2024
1 parent e3f2124 commit a3c11e5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
31 changes: 18 additions & 13 deletions pkg/properties/normalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,20 @@ type ConstValue struct {
}

type SpecParam struct {
Name *NameVariant
Description string `json:"description" yaml:"description"`
Type string `json:"type" yaml:"type"`
Default string `json:"default" yaml:"default"`
Required bool `json:"required" yaml:"required"`
Sensitive bool `json:"sensitive" yaml:"sensitive"`
Length *SpecParamLength `json:"length" yaml:"length,omitempty"`
Count *SpecParamCount `json:"count" yaml:"count,omitempty"`
Hashing *SpecParamHashing `json:"hashing" yaml:"hashing,omitempty"`
Items *SpecParamItems `json:"items" yaml:"items,omitempty"`
Regex string `json:"regex" yaml:"regex,omitempty"`
Profiles []*SpecParamProfile `json:"profiles" yaml:"profiles"`
Spec *Spec `json:"spec" yaml:"spec"`
Name *NameVariant
Description string `json:"description" yaml:"description"`
TerraformProviderConfig *SpecParamTerraformProviderConfig `json:"terraform_provider_config" yaml:"terraform_provider_config"`
Type string `json:"type" yaml:"type"`
Default string `json:"default" yaml:"default"`
Required bool `json:"required" yaml:"required"`
Sensitive bool `json:"sensitive" yaml:"sensitive"`
Length *SpecParamLength `json:"length" yaml:"length,omitempty"`
Count *SpecParamCount `json:"count" yaml:"count,omitempty"`
Hashing *SpecParamHashing `json:"hashing" yaml:"hashing,omitempty"`
Items *SpecParamItems `json:"items" yaml:"items,omitempty"`
Regex string `json:"regex" yaml:"regex,omitempty"`
Profiles []*SpecParamProfile `json:"profiles" yaml:"profiles"`
Spec *Spec `json:"spec" yaml:"spec"`
}

type SpecParamLength struct {
Expand All @@ -139,6 +140,10 @@ type SpecParamHashing struct {
Type string `json:"type" yaml:"type"`
}

type SpecParamTerraformProviderConfig struct {
Computed bool `json:"computed" yaml:"computed"`
}

type SpecParamItems struct {
Type string `json:"type" yaml:"type"`
Length *SpecParamItemsLength `json:"length" yaml:"length"`
Expand Down
24 changes: 22 additions & 2 deletions pkg/translate/terraform_provider/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,11 +776,22 @@ func createSchemaSpecForParameter(typ schemaType, structPrefix string, packageNa
LowerCamelCase: naming.CamelCase("", "name", "", false),
}

var computed, optional bool
if param.TerraformProviderConfig != nil {
computed = param.TerraformProviderConfig.Computed
optional = !computed
} else if param.Default != "" {
computed = true
optional = true
}

attributes = append(attributes, attributeCtx{
Package: packageName,
Name: name,
SchemaType: "StringAttribute",
Required: true,
Computed: computed,
Optional: optional,
})
}

Expand Down Expand Up @@ -859,17 +870,26 @@ func createSchemaAttributeForParameter(typ schemaType, packageName string, param
}
}

var optional, computed bool
if param.TerraformProviderConfig != nil {
computed = param.TerraformProviderConfig.Computed
optional = !computed
} else if param.Default != "" {
optional = true
computed = true
}

return attributeCtx{
Package: packageName,
Name: param.Name,
SchemaType: schemaType,
ElementType: elementType,
Description: param.Description,
Required: param.Required,
Optional: !param.Required,
Optional: optional,
Sensitive: param.Sensitive,
Default: defaultValue,
Computed: param.Default != "",
Computed: computed,
}
}

Expand Down
6 changes: 0 additions & 6 deletions specs/network/interface/ethernet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,6 @@ spec:
type: object
spec:
params:
- name: name
type: string
profiles:
- xpath: []
spec:
required: true
- name: enable-on-interface
type: bool
profiles:
Expand Down
6 changes: 0 additions & 6 deletions specs/network/interface/loopback.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ spec:
type: object
spec:
params:
- name: name
type: string
profiles:
- xpath: []
spec:
required: true
- name: enable-on-interface
type: bool
profiles:
Expand Down

0 comments on commit a3c11e5

Please sign in to comment.