Skip to content

Commit 7b70937

Browse files
authored
fix: Unify code responsible for upserting resources from sync subcommand (#53)
* intial conversion * mass update * make format * clean up imports * update cloudrun * nil guard * tweak error string
1 parent b8498d7 commit 7b70937

File tree

17 files changed

+130
-557
lines changed

17 files changed

+130
-557
lines changed

cmd/ctrlc/root/sync/aws/eks/eks.go

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ import (
1616
"github.com/charmbracelet/log"
1717
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/common"
1818
"github.com/ctrlplanedev/cli/internal/api"
19+
ctrlp "github.com/ctrlplanedev/cli/internal/common"
1920
"github.com/ctrlplanedev/cli/internal/kinds"
20-
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
2121
"github.com/spf13/cobra"
22-
"github.com/spf13/viper"
2322
)
2423

2524
// NewSyncEKSCmd creates a new cobra command for syncing EKS clusters
@@ -32,13 +31,13 @@ func NewSyncEKSCmd() *cobra.Command {
3231
Short: "Sync Amazon Elastic Kubernetes Service clusters into Ctrlplane",
3332
Example: heredoc.Doc(`
3433
# Make sure AWS credentials are configured via environment variables or AWS CLI
35-
34+
3635
# Sync all EKS clusters from a region
3736
$ ctrlc sync aws eks --region us-west-2
38-
37+
3938
# Sync all EKS clusters from multiple regions
4039
$ ctrlc sync aws eks --region us-west-2 --region us-east-1
41-
40+
4241
# Sync all EKS clusters from all regions
4342
$ ctrlc sync aws eks
4443
`),
@@ -117,7 +116,7 @@ func runSync(regions *[]string, name *string) func(cmd *cobra.Command, args []st
117116
common.EnsureProviderDetails(ctx, "aws-eks", regionsToSync, name)
118117

119118
// Upsert resources to Ctrlplane
120-
return upsertToCtrlplane(ctx, allResources, name)
119+
return ctrlp.UpsertResources(ctx, allResources, name)
121120
}
122121
}
123122

@@ -281,32 +280,3 @@ func initClusterMetadata(cluster *types.Cluster, region string) map[string]strin
281280

282281
return metadata
283282
}
284-
285-
func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, name *string) error {
286-
apiURL := viper.GetString("url")
287-
apiKey := viper.GetString("api-key")
288-
workspaceId := viper.GetString("workspace")
289-
290-
ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
291-
if err != nil {
292-
return fmt.Errorf("failed to create API client: %w", err)
293-
}
294-
295-
rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, *name)
296-
if err != nil {
297-
return fmt.Errorf("failed to create resource provider: %w", err)
298-
}
299-
300-
// err = rp.AddResourceRelationshipRule(ctx, relationshipRules)
301-
// if err != nil {
302-
// log.Error("Failed to add resource relationship rule", "name", *name, "error", err)
303-
// }
304-
305-
upsertResp, err := rp.UpsertResource(ctx, resources)
306-
if err != nil {
307-
return fmt.Errorf("failed to upsert resources: %w", err)
308-
}
309-
310-
log.Info("Response from upserting resources", "status", upsertResp.Status)
311-
return nil
312-
}

cmd/ctrlc/root/sync/aws/networks/networks.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import (
1414
"github.com/charmbracelet/log"
1515
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/common"
1616
"github.com/ctrlplanedev/cli/internal/api"
17-
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
17+
ctrlp "github.com/ctrlplanedev/cli/internal/common"
1818
"github.com/spf13/cobra"
19-
"github.com/spf13/viper"
2019
)
2120

2221
// NewSyncNetworksCmd creates a new cobra command for syncing AWS Networks
@@ -137,7 +136,7 @@ func runSync(regions *[]string, name *string) func(cmd *cobra.Command, args []st
137136
common.EnsureProviderDetails(ctx, "aws-networks", regionsToSync, name)
138137

139138
// Upsert resources to Ctrlplane
140-
return upsertToCtrlplane(ctx, allResources, name)
139+
return ctrlp.UpsertResources(ctx, allResources, name)
141140
}
142141
}
143142

@@ -432,28 +431,3 @@ func getSubnetName(subnet types.Subnet) string {
432431
}
433432
return subnetName
434433
}
435-
436-
// upsertToCtrlplane handles upserting resources to Ctrlplane
437-
func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, name *string) error {
438-
apiURL := viper.GetString("url")
439-
apiKey := viper.GetString("api-key")
440-
workspaceId := viper.GetString("workspace")
441-
442-
ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
443-
if err != nil {
444-
return fmt.Errorf("failed to create API client: %w", err)
445-
}
446-
447-
rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, *name)
448-
if err != nil {
449-
return fmt.Errorf("failed to create resource provider: %w", err)
450-
}
451-
452-
upsertResp, err := rp.UpsertResource(ctx, resources)
453-
if err != nil {
454-
return fmt.Errorf("failed to upsert resources: %w", err)
455-
}
456-
457-
log.Info("Response from upserting resources", "status", upsertResp.Status)
458-
return nil
459-
}

cmd/ctrlc/root/sync/aws/rds/rds.go

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import (
1414
"github.com/charmbracelet/log"
1515
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/common"
1616
"github.com/ctrlplanedev/cli/internal/api"
17+
ctrlp "github.com/ctrlplanedev/cli/internal/common"
1718
"github.com/ctrlplanedev/cli/internal/kinds"
18-
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
1919
"github.com/spf13/cobra"
20-
"github.com/spf13/viper"
2120
)
2221

2322
// NewSyncRDSCmd creates a new cobra command for syncing AWS RDS instances
@@ -30,13 +29,13 @@ func NewSyncRDSCmd() *cobra.Command {
3029
Short: "Sync Amazon Relational Database Service instances into Ctrlplane",
3130
Example: heredoc.Doc(`
3231
# Make sure AWS credentials are configured via environment variables or AWS CLI
33-
32+
3433
# Sync all RDS instances from a region
3534
$ ctrlc sync aws rds --region us-west-2
36-
35+
3736
# Sync all RDS instances from multiple regions
3837
$ ctrlc sync aws rds --region us-west-2 --region us-east-1
39-
38+
4039
# Sync all RDS instances from all regions
4140
$ ctrlc sync aws rds
4241
`),
@@ -138,7 +137,7 @@ func runSync(regions *[]string, name *string) func(cmd *cobra.Command, args []st
138137
}
139138

140139
// Upsert resources to Ctrlplane
141-
return upsertToCtrlplane(ctx, allResources, &providerRegion, name)
140+
return ctrlp.UpsertResources(ctx, allResources, name)
142141
}
143142
}
144143

@@ -513,37 +512,3 @@ func fetchParameterGroupDetails(ctx context.Context, rdsClient *rds.Client, para
513512
metadata["database/parameter-count"] = strconv.Itoa(paramCount)
514513
}
515514
}
516-
517-
// upsertToCtrlplane handles upserting resources to Ctrlplane
518-
func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, region, name *string) error {
519-
if *name == "" {
520-
*name = fmt.Sprintf("aws-rds-%s", *region)
521-
}
522-
523-
apiURL := viper.GetString("url")
524-
apiKey := viper.GetString("api-key")
525-
workspaceId := viper.GetString("workspace")
526-
527-
ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
528-
if err != nil {
529-
return fmt.Errorf("failed to create API client: %w", err)
530-
}
531-
532-
rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, *name)
533-
if err != nil {
534-
return fmt.Errorf("failed to create resource provider: %w", err)
535-
}
536-
537-
// err = rp.AddResourceRelationshipRule(ctx, relationshipRules)
538-
// if err != nil {
539-
// log.Error("Failed to add resource relationship rule", "name", *name, "error", err)
540-
// }
541-
542-
upsertResp, err := rp.UpsertResource(ctx, resources)
543-
if err != nil {
544-
return fmt.Errorf("failed to upsert resources: %w", err)
545-
}
546-
547-
log.Info("Response from upserting resources", "status", upsertResp.Status)
548-
return nil
549-
}

cmd/ctrlc/root/sync/azure/aks/aks.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
"github.com/Masterminds/semver"
1919
"github.com/charmbracelet/log"
2020
"github.com/ctrlplanedev/cli/internal/api"
21+
ctrlp "github.com/ctrlplanedev/cli/internal/common"
2122
"github.com/ctrlplanedev/cli/internal/kinds"
22-
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
2323
"github.com/spf13/cobra"
2424
"github.com/spf13/viper"
2525
)
@@ -99,7 +99,7 @@ func runSync(subscriptionID, name *string) func(cmd *cobra.Command, args []strin
9999
}
100100

101101
// Upsert resources to Ctrlplane
102-
return upsertToCtrlplane(ctx, resources, subscriptionID, name)
102+
return ctrlp.UpsertResources(ctx, resources, name)
103103
}
104104
}
105105

@@ -435,36 +435,3 @@ func extractResourceGroupFromID(id string) string {
435435
// },
436436
// },
437437
// }
438-
439-
func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, subscriptionID, name *string) error {
440-
if *name == "" {
441-
*name = fmt.Sprintf("azure-aks-%s", *subscriptionID)
442-
}
443-
444-
apiURL := viper.GetString("url")
445-
apiKey := viper.GetString("api-key")
446-
workspaceId := viper.GetString("workspace")
447-
448-
ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
449-
if err != nil {
450-
return fmt.Errorf("failed to create API client: %w", err)
451-
}
452-
453-
rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, *name)
454-
if err != nil {
455-
return fmt.Errorf("failed to create resource provider: %w", err)
456-
}
457-
458-
// err = rp.AddResourceRelationshipRule(ctx, relationshipRules)
459-
// if err != nil {
460-
// log.Error("Failed to add resource relationship rule", "name", *name, "error", err)
461-
// }
462-
463-
upsertResp, err := rp.UpsertResource(ctx, resources)
464-
if err != nil {
465-
return fmt.Errorf("failed to upsert resources: %w", err)
466-
}
467-
468-
log.Info("Response from upserting resources", "status", upsertResp.Status)
469-
return nil
470-
}

cmd/ctrlc/root/sync/azure/networks/networks.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
"github.com/charmbracelet/log"
1717
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/azure/common"
1818
"github.com/ctrlplanedev/cli/internal/api"
19+
ctrlp "github.com/ctrlplanedev/cli/internal/common"
1920
"github.com/ctrlplanedev/cli/internal/kinds"
20-
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
2121
"github.com/spf13/cobra"
2222
"github.com/spf13/viper"
2323
)
@@ -95,7 +95,7 @@ func runSync(subscriptionID, name *string) func(cmd *cobra.Command, args []strin
9595
}
9696

9797
// Upsert resources to Ctrlplane
98-
return upsertToCtrlplane(ctx, resources, subscriptionID, name)
98+
return ctrlp.UpsertResources(ctx, resources, name)
9999
}
100100
}
101101

@@ -404,31 +404,3 @@ func getSubnetState(subnet *armnetwork.Subnet) string {
404404
}
405405
return ""
406406
}
407-
408-
func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, subscriptionID, name *string) error {
409-
if *name == "" {
410-
*name = fmt.Sprintf("azure-networks-%s", *subscriptionID)
411-
}
412-
413-
apiURL := viper.GetString("url")
414-
apiKey := viper.GetString("api-key")
415-
workspaceId := viper.GetString("workspace")
416-
417-
ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
418-
if err != nil {
419-
return fmt.Errorf("failed to create API client: %w", err)
420-
}
421-
422-
rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, *name)
423-
if err != nil {
424-
return fmt.Errorf("failed to create resource provider: %w", err)
425-
}
426-
427-
upsertResp, err := rp.UpsertResource(ctx, resources)
428-
if err != nil {
429-
return fmt.Errorf("failed to upsert resources: %w", err)
430-
}
431-
432-
log.Info("Response from upserting resources", "status", upsertResp.Status)
433-
return nil
434-
}

cmd/ctrlc/root/sync/github/pullrequests.go

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import (
1111
"github.com/MakeNowJust/heredoc/v2"
1212
"github.com/charmbracelet/log"
1313
"github.com/ctrlplanedev/cli/internal/api"
14+
ctrlp "github.com/ctrlplanedev/cli/internal/common"
1415
"github.com/ctrlplanedev/cli/internal/kinds"
15-
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
1616
"github.com/google/go-github/v57/github"
1717
"github.com/spf13/cobra"
18-
"github.com/spf13/viper"
1918
"golang.org/x/oauth2"
2019
)
2120

@@ -161,9 +160,17 @@ func runSync(repoPath, token, name *string, states *[]string) func(cmd *cobra.Co
161160
}
162161
log.Debug("Pull requests processed successfully", "count", len(resources))
163162

163+
// Set default provider name if not provided
164+
if *name == "" {
165+
*name = fmt.Sprintf("github-prs-%s-%s", owner, repo)
166+
log.Debug("Using generated provider name", "name", *name)
167+
} else {
168+
log.Debug("Using provided provider name", "name", *name)
169+
}
170+
164171
// Upsert resources to Ctrlplane
165172
log.Debug("Upserting resources to Ctrlplane", "count", len(resources))
166-
return upsertToCtrlplane(ctx, resources, owner, repo, *name)
173+
return ctrlp.UpsertResources(ctx, resources, name)
167174
}
168175
}
169176

@@ -607,54 +614,3 @@ func initPullRequestMetadata(pr *github.PullRequest, owner, repo string) map[str
607614
}
608615

609616
// var relationshipRules = []api.Relationship{}
610-
611-
// upsertToCtrlplane handles upserting resources to Ctrlplane
612-
func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, owner, repo, name string) error {
613-
log.Debug("Upserting resources to Ctrlplane", "count", len(resources))
614-
615-
if name == "" {
616-
name = fmt.Sprintf("github-prs-%s-%s", owner, repo)
617-
log.Debug("Using generated provider name", "name", name)
618-
} else {
619-
log.Debug("Using provided provider name", "name", name)
620-
}
621-
622-
apiURL := viper.GetString("url")
623-
apiKey := viper.GetString("api-key")
624-
workspaceId := viper.GetString("workspace")
625-
626-
log.Debug("API configuration", "url", apiURL, "workspace", workspaceId)
627-
628-
log.Debug("Creating API client")
629-
ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
630-
if err != nil {
631-
log.Error("Failed to create API client", "error", err)
632-
return fmt.Errorf("failed to create API client: %w", err)
633-
}
634-
635-
log.Debug("Creating resource provider", "name", name)
636-
rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, name)
637-
if err != nil {
638-
log.Error("Failed to create resource provider", "name", name, "error", err)
639-
return fmt.Errorf("failed to create resource provider: %w", err)
640-
}
641-
642-
// log.Debug("Adding resource relationship rules", "rules_count", len(relationshipRules))
643-
// err = rp.AddResourceRelationshipRule(ctx, relationshipRules)
644-
// if err != nil {
645-
// log.Error("Failed to add resource relationship rule", "name", name, "error", err)
646-
// } else {
647-
// log.Debug("Successfully added relationship rules")
648-
// }
649-
650-
log.Debug("Upserting resources", "count", len(resources))
651-
upsertResp, err := rp.UpsertResource(ctx, resources)
652-
if err != nil {
653-
log.Error("Failed to upsert resources", "error", err)
654-
return fmt.Errorf("failed to upsert resources: %w", err)
655-
}
656-
657-
log.Info("Response from upserting resources", "status", upsertResp.Status)
658-
log.Debug("Successfully upserted resources to Ctrlplane")
659-
return nil
660-
}

0 commit comments

Comments
 (0)