Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 5 additions & 35 deletions cmd/ctrlc/root/sync/aws/eks/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import (
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/common"
"github.com/ctrlplanedev/cli/internal/api"
ctrlp "github.com/ctrlplanedev/cli/internal/common"
"github.com/ctrlplanedev/cli/internal/kinds"
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// NewSyncEKSCmd creates a new cobra command for syncing EKS clusters
Expand All @@ -32,13 +31,13 @@ func NewSyncEKSCmd() *cobra.Command {
Short: "Sync Amazon Elastic Kubernetes Service clusters into Ctrlplane",
Example: heredoc.Doc(`
# Make sure AWS credentials are configured via environment variables or AWS CLI

# Sync all EKS clusters from a region
$ ctrlc sync aws eks --region us-west-2

# Sync all EKS clusters from multiple regions
$ ctrlc sync aws eks --region us-west-2 --region us-east-1

# Sync all EKS clusters from all regions
$ ctrlc sync aws eks
`),
Expand Down Expand Up @@ -117,7 +116,7 @@ func runSync(regions *[]string, name *string) func(cmd *cobra.Command, args []st
common.EnsureProviderDetails(ctx, "aws-eks", regionsToSync, name)

// Upsert resources to Ctrlplane
return upsertToCtrlplane(ctx, allResources, name)
return ctrlp.UpsertResources(ctx, allResources, name)
}
}

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

return metadata
}

func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, name *string) error {
apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspaceId := viper.GetString("workspace")

ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}

rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, *name)
if err != nil {
return fmt.Errorf("failed to create resource provider: %w", err)
}

// err = rp.AddResourceRelationshipRule(ctx, relationshipRules)
// if err != nil {
// log.Error("Failed to add resource relationship rule", "name", *name, "error", err)
// }

upsertResp, err := rp.UpsertResource(ctx, resources)
if err != nil {
return fmt.Errorf("failed to upsert resources: %w", err)
}

log.Info("Response from upserting resources", "status", upsertResp.Status)
return nil
}
30 changes: 2 additions & 28 deletions cmd/ctrlc/root/sync/aws/networks/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/common"
"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
ctrlp "github.com/ctrlplanedev/cli/internal/common"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

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

// Upsert resources to Ctrlplane
return upsertToCtrlplane(ctx, allResources, name)
return ctrlp.UpsertResources(ctx, allResources, name)
}
}

Expand Down Expand Up @@ -432,28 +431,3 @@ func getSubnetName(subnet types.Subnet) string {
}
return subnetName
}

// upsertToCtrlplane handles upserting resources to Ctrlplane
func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, name *string) error {
apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspaceId := viper.GetString("workspace")

ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}

rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, *name)
if err != nil {
return fmt.Errorf("failed to create resource provider: %w", err)
}

upsertResp, err := rp.UpsertResource(ctx, resources)
if err != nil {
return fmt.Errorf("failed to upsert resources: %w", err)
}

log.Info("Response from upserting resources", "status", upsertResp.Status)
return nil
}
45 changes: 5 additions & 40 deletions cmd/ctrlc/root/sync/aws/rds/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/common"
"github.com/ctrlplanedev/cli/internal/api"
ctrlp "github.com/ctrlplanedev/cli/internal/common"
"github.com/ctrlplanedev/cli/internal/kinds"
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// NewSyncRDSCmd creates a new cobra command for syncing AWS RDS instances
Expand All @@ -30,13 +29,13 @@ func NewSyncRDSCmd() *cobra.Command {
Short: "Sync Amazon Relational Database Service instances into Ctrlplane",
Example: heredoc.Doc(`
# Make sure AWS credentials are configured via environment variables or AWS CLI

# Sync all RDS instances from a region
$ ctrlc sync aws rds --region us-west-2

# Sync all RDS instances from multiple regions
$ ctrlc sync aws rds --region us-west-2 --region us-east-1

# Sync all RDS instances from all regions
$ ctrlc sync aws rds
`),
Expand Down Expand Up @@ -138,7 +137,7 @@ func runSync(regions *[]string, name *string) func(cmd *cobra.Command, args []st
}

// Upsert resources to Ctrlplane
return upsertToCtrlplane(ctx, allResources, &providerRegion, name)
return ctrlp.UpsertResources(ctx, allResources, name)
}
}

Expand Down Expand Up @@ -513,37 +512,3 @@ func fetchParameterGroupDetails(ctx context.Context, rdsClient *rds.Client, para
metadata["database/parameter-count"] = strconv.Itoa(paramCount)
}
}

// upsertToCtrlplane handles upserting resources to Ctrlplane
func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, region, name *string) error {
if *name == "" {
*name = fmt.Sprintf("aws-rds-%s", *region)
}

apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspaceId := viper.GetString("workspace")

ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}

rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, *name)
if err != nil {
return fmt.Errorf("failed to create resource provider: %w", err)
}

// err = rp.AddResourceRelationshipRule(ctx, relationshipRules)
// if err != nil {
// log.Error("Failed to add resource relationship rule", "name", *name, "error", err)
// }

upsertResp, err := rp.UpsertResource(ctx, resources)
if err != nil {
return fmt.Errorf("failed to upsert resources: %w", err)
}

log.Info("Response from upserting resources", "status", upsertResp.Status)
return nil
}
37 changes: 2 additions & 35 deletions cmd/ctrlc/root/sync/azure/aks/aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/Masterminds/semver"
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/internal/api"
ctrlp "github.com/ctrlplanedev/cli/internal/common"
"github.com/ctrlplanedev/cli/internal/kinds"
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -99,7 +99,7 @@ func runSync(subscriptionID, name *string) func(cmd *cobra.Command, args []strin
}

// Upsert resources to Ctrlplane
return upsertToCtrlplane(ctx, resources, subscriptionID, name)
return ctrlp.UpsertResources(ctx, resources, name)
}
}

Expand Down Expand Up @@ -435,36 +435,3 @@ func extractResourceGroupFromID(id string) string {
// },
// },
// }

func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, subscriptionID, name *string) error {
if *name == "" {
*name = fmt.Sprintf("azure-aks-%s", *subscriptionID)
}

apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspaceId := viper.GetString("workspace")

ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}

rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, *name)
if err != nil {
return fmt.Errorf("failed to create resource provider: %w", err)
}

// err = rp.AddResourceRelationshipRule(ctx, relationshipRules)
// if err != nil {
// log.Error("Failed to add resource relationship rule", "name", *name, "error", err)
// }

upsertResp, err := rp.UpsertResource(ctx, resources)
if err != nil {
return fmt.Errorf("failed to upsert resources: %w", err)
}

log.Info("Response from upserting resources", "status", upsertResp.Status)
return nil
}
32 changes: 2 additions & 30 deletions cmd/ctrlc/root/sync/azure/networks/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/azure/common"
"github.com/ctrlplanedev/cli/internal/api"
ctrlp "github.com/ctrlplanedev/cli/internal/common"
"github.com/ctrlplanedev/cli/internal/kinds"
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -95,7 +95,7 @@ func runSync(subscriptionID, name *string) func(cmd *cobra.Command, args []strin
}

// Upsert resources to Ctrlplane
return upsertToCtrlplane(ctx, resources, subscriptionID, name)
return ctrlp.UpsertResources(ctx, resources, name)
}
}

Expand Down Expand Up @@ -404,31 +404,3 @@ func getSubnetState(subnet *armnetwork.Subnet) string {
}
return ""
}

func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, subscriptionID, name *string) error {
if *name == "" {
*name = fmt.Sprintf("azure-networks-%s", *subscriptionID)
}

apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspaceId := viper.GetString("workspace")

ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}

rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, *name)
if err != nil {
return fmt.Errorf("failed to create resource provider: %w", err)
}

upsertResp, err := rp.UpsertResource(ctx, resources)
if err != nil {
return fmt.Errorf("failed to upsert resources: %w", err)
}

log.Info("Response from upserting resources", "status", upsertResp.Status)
return nil
}
64 changes: 10 additions & 54 deletions cmd/ctrlc/root/sync/github/pullrequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import (
"github.com/MakeNowJust/heredoc/v2"
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/internal/api"
ctrlp "github.com/ctrlplanedev/cli/internal/common"
"github.com/ctrlplanedev/cli/internal/kinds"
"github.com/ctrlplanedev/cli/pkg/resourceprovider"
"github.com/google/go-github/v57/github"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/oauth2"
)

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

// Set default provider name if not provided
if *name == "" {
*name = fmt.Sprintf("github-prs-%s-%s", owner, repo)
log.Debug("Using generated provider name", "name", *name)
} else {
log.Debug("Using provided provider name", "name", *name)
}

// Upsert resources to Ctrlplane
log.Debug("Upserting resources to Ctrlplane", "count", len(resources))
return upsertToCtrlplane(ctx, resources, owner, repo, *name)
return ctrlp.UpsertResources(ctx, resources, name)
}
}

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

// var relationshipRules = []api.Relationship{}

// upsertToCtrlplane handles upserting resources to Ctrlplane
func upsertToCtrlplane(ctx context.Context, resources []api.ResourceProviderResource, owner, repo, name string) error {
log.Debug("Upserting resources to Ctrlplane", "count", len(resources))

if name == "" {
name = fmt.Sprintf("github-prs-%s-%s", owner, repo)
log.Debug("Using generated provider name", "name", name)
} else {
log.Debug("Using provided provider name", "name", name)
}

apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspaceId := viper.GetString("workspace")

log.Debug("API configuration", "url", apiURL, "workspace", workspaceId)

log.Debug("Creating API client")
ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
log.Error("Failed to create API client", "error", err)
return fmt.Errorf("failed to create API client: %w", err)
}

log.Debug("Creating resource provider", "name", name)
rp, err := resourceprovider.New(ctrlplaneClient, workspaceId, name)
if err != nil {
log.Error("Failed to create resource provider", "name", name, "error", err)
return fmt.Errorf("failed to create resource provider: %w", err)
}

// log.Debug("Adding resource relationship rules", "rules_count", len(relationshipRules))
// err = rp.AddResourceRelationshipRule(ctx, relationshipRules)
// if err != nil {
// log.Error("Failed to add resource relationship rule", "name", name, "error", err)
// } else {
// log.Debug("Successfully added relationship rules")
// }

log.Debug("Upserting resources", "count", len(resources))
upsertResp, err := rp.UpsertResource(ctx, resources)
if err != nil {
log.Error("Failed to upsert resources", "error", err)
return fmt.Errorf("failed to upsert resources: %w", err)
}

log.Info("Response from upserting resources", "status", upsertResp.Status)
log.Debug("Successfully upserted resources to Ctrlplane")
return nil
}
Loading