Skip to content

Commit

Permalink
fix: external names
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezcuesta committed Nov 13, 2024
1 parent 5c0b5e0 commit cf3de03
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 66 deletions.
16 changes: 4 additions & 12 deletions config/actions/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package actions

import (
"context"
"fmt"

c "github.com/crossplane-contrib/provider-pagerduty/config/common"
"github.com/crossplane/upjet/pkg/config"
)

Expand All @@ -19,9 +17,7 @@ func Configure(p *config.Provider) {
p.AddResourceConfigurator("pagerduty_automation_actions_action_service_association", func(r *config.Resource) {
r.ShortGroup = ShortGroup
r.Kind = "ActionServiceAssociation"
r.ExternalName.GetIDFn = func(_ context.Context, externalName string, parameters map[string]any, _ map[string]any) (string, error) {
return fmt.Sprintf("%s:%s", parameters["action_id"].(string), parameters["service_id"].(string)), nil
}
r.ExternalName = c.ExternalNameFromParams([]string{"action_id", "service_id"})
r.References = config.References{
"action_id": {
Type: "Action",
Expand All @@ -38,9 +34,7 @@ func Configure(p *config.Provider) {
p.AddResourceConfigurator("pagerduty_automation_actions_action_team_association", func(r *config.Resource) {
r.ShortGroup = ShortGroup
r.Kind = "ActionTeamAssociation"
r.ExternalName.GetIDFn = func(_ context.Context, externalName string, parameters map[string]any, _ map[string]any) (string, error) {
return fmt.Sprintf("%s:%s", parameters["action_id"].(string), parameters["team_id"].(string)), nil
}
r.ExternalName = c.ExternalNameFromParams([]string{"action_id", "team_id"})
r.References = config.References{
"action_id": {
Type: "Action",
Expand All @@ -61,9 +55,7 @@ func Configure(p *config.Provider) {
p.AddResourceConfigurator("pagerduty_automation_actions_runner_team_association", func(r *config.Resource) {
r.ShortGroup = ShortGroup
r.Kind = "RunnerTeamAssociation"
r.ExternalName.GetIDFn = func(_ context.Context, externalName string, parameters map[string]any, _ map[string]any) (string, error) {
return fmt.Sprintf("%s:%s", parameters["runner_id"].(string), parameters["team_id"].(string)), nil
}
r.ExternalName = c.ExternalNameFromParams([]string{"runner_id", "team_id"})
r.References = config.References{
"runner_id": {
Type: "github.com/crossplane-contrib/provider-pagerduty/apis/automation/v1alpha1.Runner",
Expand Down
6 changes: 3 additions & 3 deletions config/business/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package business

import (
"github.com/crossplane-contrib/provider-pagerduty/config/fake"
c "github.com/crossplane-contrib/provider-pagerduty/config/common"
"github.com/crossplane/upjet/pkg/config"
)

Expand All @@ -15,8 +15,8 @@ func Configure(p *config.Provider) {
Type: "github.com/crossplane-contrib/provider-pagerduty/apis/team/v1alpha1.Team",
},
}
r.ExternalName.GetExternalNameFn = fake.GetExternalName
r.ExternalName.GetIDFn = fake.GetID
r.ExternalName.GetExternalNameFn = c.GetExternalName
r.ExternalName.GetIDFn = c.GetFakeID
// Deprecated
if s, ok := r.TerraformResource.Schema["type"]; ok {
s.Optional = false
Expand Down
56 changes: 56 additions & 0 deletions config/common/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package commmon

import (
"context"
"strings"

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

// Errors
const (
ErrFmtNoAttribute = `attribute not found: %s`
ErrFmtUnexpectedType = `unexpected type for attribute %s: Expecting a string`
Managed = `managed`
)

func GetExternalName(tfstate map[string]any) (string, error) {
id, ok := tfstate["id"]
if !ok {
return "", errors.Errorf(ErrFmtNoAttribute, "id")
}
idStr, ok := id.(string)
if !ok {
return "", errors.Errorf(ErrFmtUnexpectedType, "id")
}
return idStr, nil
}

func GetFakeID(_ context.Context, externalName string, _ map[string]any, _ map[string]any) (string, error) {
if externalName == "" {
return Managed, nil
}
return externalName, nil
}

func SplitExternalNameFromId() config.ExternalName {
e := config.IdentifierFromProvider
e.GetExternalNameFn = func(tfstate map[string]interface{}) (string, error) {
id, ok := tfstate["id"]
if !ok {
return "", errors.New("id in tfstate cannot be empty")
}
w := strings.Split(id.(string), ":")
return w[len(w)-1], nil
}
return e
}

func ExternalNameFromParams(params []string) config.ExternalName {
e := config.IdentifierFromProvider
e.GetExternalNameFn = func(tfstate map[string]interface{}) (string, error) {
return strings.Join(params, ":"), nil
}
return e
}
21 changes: 6 additions & 15 deletions config/event/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package event

import (
"context"
"fmt"

c "github.com/crossplane-contrib/provider-pagerduty/config/common"
"github.com/crossplane/upjet/pkg/config"
)

Expand Down Expand Up @@ -52,6 +50,7 @@ func Configure(p *config.Provider) {
p.AddResourceConfigurator("pagerduty_event_orchestration", func(r *config.Resource) {

r.ShortGroup = ShortGroup
r.ExternalName = c.SplitExternalNameFromId()
r.References = config.References{
"team": {
Type: "github.com/crossplane-contrib/provider-pagerduty/apis/team/v1alpha1.Team",
Expand All @@ -62,9 +61,7 @@ func Configure(p *config.Provider) {
p.AddResourceConfigurator("pagerduty_event_orchestration_global_cache_variable", func(r *config.Resource) {

r.ShortGroup = ShortGroup
r.ExternalName.GetIDFn = func(_ context.Context, externalName string, parameters map[string]any, _ map[string]any) (string, error) {
return fmt.Sprintf("%s:%s", parameters["event_orchestration"].(string), externalName), nil
}
r.ExternalName = c.SplitExternalNameFromId()
r.References = config.References{
"event_orchestration": {
Type: "Orchestration",
Expand All @@ -75,9 +72,7 @@ func Configure(p *config.Provider) {
p.AddResourceConfigurator("pagerduty_event_orchestration_global", func(r *config.Resource) {

r.ShortGroup = ShortGroup
r.ExternalName.GetIDFn = func(_ context.Context, externalName string, parameters map[string]any, _ map[string]any) (string, error) {
return fmt.Sprintf("%s:%s", parameters["event_orchestration"].(string), externalName), nil
}
r.ExternalName = c.SplitExternalNameFromId()
r.References = config.References{
"event_orchestration": {
Type: "Orchestration",
Expand All @@ -88,9 +83,7 @@ func Configure(p *config.Provider) {
p.AddResourceConfigurator("pagerduty_event_orchestration_integration", func(r *config.Resource) {

r.ShortGroup = ShortGroup
r.ExternalName.GetIDFn = func(_ context.Context, externalName string, parameters map[string]any, _ map[string]any) (string, error) {
return fmt.Sprintf("%s:%s", parameters["event_orchestration"].(string), externalName), nil
}
r.ExternalName = c.SplitExternalNameFromId()
r.References = config.References{
"event_orchestration": {
Type: "Orchestration",
Expand All @@ -101,9 +94,7 @@ func Configure(p *config.Provider) {
p.AddResourceConfigurator("pagerduty_event_orchestration_service_cache_variable", func(r *config.Resource) {

r.ShortGroup = ShortGroup
r.ExternalName.GetIDFn = func(_ context.Context, externalName string, parameters map[string]any, _ map[string]any) (string, error) {
return fmt.Sprintf("%s:%s", parameters["service"].(string), externalName), nil
}
r.ExternalName = c.SplitExternalNameFromId()
r.References = config.References{
"service": {
Type: "OrchestrationService",
Expand Down
33 changes: 0 additions & 33 deletions config/fake/config.go

This file was deleted.

6 changes: 3 additions & 3 deletions config/team/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package team

import (
"github.com/crossplane-contrib/provider-pagerduty/config/fake"
c "github.com/crossplane-contrib/provider-pagerduty/config/common"
"github.com/crossplane/upjet/pkg/config"
)

// Configure configures individual resources by adding custom ResourceConfigurators.
func Configure(p *config.Provider) {
p.AddResourceConfigurator("pagerduty_team", func(r *config.Resource) {
r.ExternalName = config.IdentifierFromProvider
r.ExternalName.GetExternalNameFn = fake.GetExternalName
r.ExternalName.GetIDFn = fake.GetID
r.ExternalName.GetExternalNameFn = c.GetExternalName
r.ExternalName.GetIDFn = c.GetFakeID
r.ShortGroup = "team"
})

Expand Down

0 comments on commit cf3de03

Please sign in to comment.