Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #160 from ulucinar/fix-154
Browse files Browse the repository at this point in the history
Make Terraform resource ID available in status.atProvider
  • Loading branch information
ulucinar authored Dec 2, 2021
2 parents dd4adc6 + caccef5 commit a0f25d4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/pipeline/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
twtypes "github.com/muvaf/typewriter/pkg/types"
"github.com/muvaf/typewriter/pkg/wrapper"
"github.com/pkg/errors"
Expand Down Expand Up @@ -66,6 +67,12 @@ func (cg *CRDGenerator) Generate(cfg *config.Resource) (string, error) {
for _, omit := range cfg.ExternalName.OmittedFields {
delete(cfg.TerraformResource.Schema, omit)
}

cfg.TerraformResource.Schema["id"] = &schema.Schema{
Type: schema.TypeString,
Computed: true,
}

gen, err := tjtypes.NewBuilder(cg.pkg).Build(cfg)
if err != nil {
return "", errors.Wrapf(err, "cannot build types for %s", cfg.Kind)
Expand Down
8 changes: 8 additions & 0 deletions pkg/pipeline/templates/terraformed.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ func (tr *{{ .CRD.Kind }}) SetObservation(obs map[string]interface{}) error {
return json.TFParser.Unmarshal(p, &tr.Status.AtProvider)
}

// GetObservation of this {{ .CRD.Kind }}
func (tr *{{ .CRD.Kind }}) GetID() string {
if tr.Status.AtProvider.ID == nil {
return ""
}
return *tr.Status.AtProvider.ID
}

// GetParameters of this {{ .CRD.Kind }}
func (tr *{{ .CRD.Kind }}) GetParameters() (map[string]interface{}, error) {
p, err := json.TFParser.Marshal(tr.Spec.ForProvider)
Expand Down
6 changes: 6 additions & 0 deletions pkg/resource/fake/terraformed.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
type Observable struct {
Observation map[string]interface{}
AdditionalConnectionDetails map[string][]byte
ID string
}

// GetObservation is a mock.
Expand All @@ -41,6 +42,11 @@ func (o *Observable) SetObservation(data map[string]interface{}) error {
return nil
}

// GetID is a mock.
func (o *Observable) GetID() string {
return o.ID
}

// GetAdditionalConnectionDetails is a mock
func (o *Observable) GetAdditionalConnectionDetails(_ map[string]interface{}) (map[string][]byte, error) {
return o.AdditionalConnectionDetails, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/resource/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
type Observable interface {
GetObservation() (map[string]interface{}, error)
SetObservation(map[string]interface{}) error
GetID() string
}

// Parameterizable structs can get and set parameters of the managed resource
Expand Down

0 comments on commit a0f25d4

Please sign in to comment.