Skip to content

Commit

Permalink
fix(tags): import of tag resources failed with 403 error
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Levée <[email protected]>
  • Loading branch information
nlevee committed Sep 16, 2024
1 parent f528eed commit 4170db9
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 3 deletions.
22 changes: 19 additions & 3 deletions config/externalname.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package config

import (
"strings"

"github.com/crossplane/upjet/pkg/config"

"github.com/upbound/provider-gcp/config/common"
Expand Down Expand Up @@ -1012,18 +1014,32 @@ var terraformPluginSDKExternalNameConfigs = map[string]config.ExternalName{
// tags
//
// Imported by using the following tagBindings/{{name}}
"google_tags_tag_binding": config.IdentifierFromProvider,
// there is an exception here because terraform SDK wait for a 'name' without tagBindings/
"google_tags_tag_binding": NameAsIdentifierWithTrimExternalNamePrefix("tagBindings/"),
// Imported by using the following tagKeys/{{name}}
"google_tags_tag_key": config.IdentifierFromProvider,
// there is an exception here because terraform SDK wait for a 'name' without tagKeys/
"google_tags_tag_key": NameAsIdentifierWithTrimExternalNamePrefix("tagKeys/"),
// Imported by using the following tagValues/{{name}}
"google_tags_tag_value": config.IdentifierFromProvider,
// there is an exception here because terraform SDK wait for a 'name' without tagValues/
"google_tags_tag_value": NameAsIdentifierWithTrimExternalNamePrefix("tagValues/"),
}

// cliReconciledExternalNameConfigs contains all external name configurations
// belonging to Terraform resources to be reconciled under the CLI-based
// architecture for this provider.
var cliReconciledExternalNameConfigs = map[string]config.ExternalName{}

// NameAsIdentifierWithTrimExternalNamePrefix uses NameAsIdentifier but
// trimLeft "prefix" from externalName. It allow to remove prefix in externalName
// when Terraform provider need it
func NameAsIdentifierWithTrimExternalNamePrefix(prefix string) config.ExternalName {
return config.NewExternalNameFrom(config.NameAsIdentifier, config.WithSetIdentifierArgumentsFn(
func(fn config.SetIdentifierArgumentsFn, base map[string]any, externalName string) {
externalName = strings.TrimLeft(externalName, prefix)
fn(base, externalName)
}))
}

// TemplatedStringAsIdentifierWithNoName uses TemplatedStringAsIdentifier but
// without the name initializer. This allows it to be used in cases where the ID
// is constructed with parameters and a provider-defined value, meaning no
Expand Down
3 changes: 3 additions & 0 deletions config/tags/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ func Configure(p *config.Provider) {
TerraformName: "google_tags_tag_value",
Extractor: common.ExtractResourceIDFuncPath,
}
config.MarkAsRequired(r.TerraformResource, "parent", "tag_value")
})
p.AddResourceConfigurator("google_tags_tag_key", func(r *config.Resource) {
r.UseAsync = true
config.MarkAsRequired(r.TerraformResource, "parent", "short_name")
})
p.AddResourceConfigurator("google_tags_tag_value", func(r *config.Resource) {
r.UseAsync = true
r.References["parent"] = config.Reference{
TerraformName: "google_tags_tag_key",
Extractor: common.ExtractResourceIDFuncPath,
}
config.MarkAsRequired(r.TerraformResource, "parent", "short_name")
})
}
16 changes: 16 additions & 0 deletions examples/tags/v1beta1/tagbinding.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
apiVersion: tags.gcp.upbound.io/v1beta1
kind: TagBinding
metadata:
annotations:
meta.upbound.io/example-id: tags/v1beta1/tagbinding
upjet.upbound.io/manual-intervention: "The resource requires a real external name"
crossplane.io/external-name: "&{tagBindingID}"
labels:
testing.upbound.io/example-name: binding
name: binding-external
spec:
managementPolicies: ["Observe"]
forProvider: {}

---

apiVersion: tags.gcp.upbound.io/v1beta1
kind: TagBinding
metadata:
Expand Down
16 changes: 16 additions & 0 deletions examples/tags/v1beta1/tagkey.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
apiVersion: tags.gcp.upbound.io/v1beta1
kind: TagKey
metadata:
annotations:
meta.upbound.io/example-id: tags/v1beta1/tagkey
upjet.upbound.io/manual-intervention: "The resource requires a real external name"
crossplane.io/external-name: "tagKeys/281475435132414"
labels:
testing.upbound.io/example-name: key-external
name: key-external
spec:
managementPolicies: ["Observe"]
forProvider: {}

---

apiVersion: tags.gcp.upbound.io/v1beta1
kind: TagKey
metadata:
Expand Down
16 changes: 16 additions & 0 deletions examples/tags/v1beta1/tagvalue.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
apiVersion: tags.gcp.upbound.io/v1beta1
kind: TagValue
metadata:
annotations:
meta.upbound.io/example-id: tags/v1beta1/tagvalue
upjet.upbound.io/manual-intervention: "The resource requires a real external name"
crossplane.io/external-name: "&{tagValueID}"
labels:
testing.upbound.io/example-name: value-external
name: value-external
spec:
managementPolicies: ["Observe"]
forProvider: {}

---

apiVersion: tags.gcp.upbound.io/v1beta1
kind: TagValue
metadata:
Expand Down
1 change: 1 addition & 0 deletions internal/controller/tags/tagbinding/zz_controller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/controller/tags/tagkey/zz_controller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/controller/tags/tagvalue/zz_controller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4170db9

Please sign in to comment.