Skip to content

Commit 07667d7

Browse files
committed
add ability to parse variables as part of apply command
1 parent a136012 commit 07667d7

File tree

22 files changed

+166
-156
lines changed

22 files changed

+166
-156
lines changed

cmd/ctrlc/root/api/upsert/resource/resource.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ func NewUpsertResourceCmd() *cobra.Command {
6161
sensitive := false
6262
vv := api.Variable{}
6363
dvv := api.DirectVariable_Value{}
64-
dvv.SetString(v)
64+
valueData, _ := json.Marshal(v)
65+
dvv.UnmarshalJSON(valueData)
6566
vv.FromDirectVariable(api.DirectVariable{
6667
Key: k,
6768
Sensitive: &sensitive,

cmd/ctrlc/root/apply/resource.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package apply
22

33
import (
44
"context"
5+
"encoding/json"
56

67
"github.com/charmbracelet/log"
78
"github.com/ctrlplanedev/cli/internal/api"
@@ -19,15 +20,69 @@ func processResourceProvider(ctx context.Context, client *api.ClientWithResponse
1920
return
2021
}
2122

22-
resources := make([]api.AgentResource, 0)
23+
resources := make([]api.CreateResource, 0)
2324
for _, resource := range provider.Resources {
24-
resources = append(resources, api.AgentResource{
25+
26+
vars := make([]api.Variable, 0)
27+
if resource.Variables != nil {
28+
for _, variable := range *resource.Variables {
29+
if variable.Reference != nil {
30+
if variable.Path == nil || len(*variable.Path) == 0 {
31+
log.Error(
32+
"Reference variable must have a path",
33+
"name", resource.Name,
34+
"key", variable.Key,
35+
"reference", *variable.Reference,
36+
)
37+
continue
38+
}
39+
pathValue := []string{}
40+
if variable.Path != nil {
41+
pathValue = *variable.Path
42+
}
43+
refVar := api.ReferenceVariable{
44+
Key: variable.Key,
45+
Reference: *variable.Reference,
46+
Path: pathValue,
47+
DefaultValue: nil,
48+
}
49+
50+
if variable.DefaultValue != nil {
51+
refVar.DefaultValue = &api.ReferenceVariable_DefaultValue{}
52+
valueData, _ := json.Marshal(*variable.DefaultValue)
53+
refVar.DefaultValue.UnmarshalJSON(valueData)
54+
}
55+
56+
var varRef api.Variable
57+
varRef.FromReferenceVariable(refVar)
58+
vars = append(vars, varRef)
59+
}
60+
61+
if variable.Value != nil {
62+
directVar := api.DirectVariable{
63+
Key: variable.Key,
64+
Value: api.DirectVariable_Value{},
65+
}
66+
67+
// Set the value based on type
68+
valueData, _ := json.Marshal(*variable.Value)
69+
directVar.Value.UnmarshalJSON(valueData)
70+
71+
var varDirect api.Variable
72+
varDirect.FromDirectVariable(directVar)
73+
vars = append(vars, varDirect)
74+
}
75+
}
76+
}
77+
78+
resources = append(resources, api.CreateResource{
2579
Identifier: resource.Identifier,
2680
Name: resource.Name,
2781
Version: resource.Version,
2882
Kind: resource.Kind,
2983
Config: resource.Config,
3084
Metadata: resource.Metadata,
85+
Variables: &vars,
3186
})
3287
}
3388

cmd/ctrlc/root/apply/types.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,24 @@ type ResourceProvider struct {
4141
Resources []Resource `yaml:"resources"`
4242
}
4343

44+
type Variable struct {
45+
DefaultValue *any `yaml:"defaultValue,omitempty"`
46+
Reference *string `yaml:"reference,omitempty"`
47+
Path *[]string `yaml:"path,omitempty"`
48+
49+
Key string `yaml:"key"`
50+
Sensitive *bool `yaml:"sensitive,omitempty"`
51+
Value *any `yaml:"value,omitempty"`
52+
}
53+
4454
type Resource struct {
4555
Identifier string `yaml:"identifier"`
4656
Name string `yaml:"name"`
4757
Version string `yaml:"version"`
4858
Kind string `yaml:"kind"`
4959
Config map[string]any `yaml:"config"`
5060
Metadata map[string]string `yaml:"metadata"`
51-
Variables map[string]any `yaml:"variables"`
61+
Variables *[]Variable `yaml:"variables,omitempty"`
5262
}
5363

5464
type TargetResource struct {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func NewSyncEC2Cmd() *cobra.Command {
9797
return fmt.Errorf("failed to describe instances: %w", err)
9898
}
9999

100-
resources := []api.AgentResource{}
100+
resources := []api.CreateResource{}
101101
for _, reservation := range result.Reservations {
102102
accountId := *reservation.OwnerId
103103
for _, instance := range reservation.Instances {
@@ -231,7 +231,7 @@ func NewSyncEC2Cmd() *cobra.Command {
231231

232232
// Get ARN for the instance
233233
arn := fmt.Sprintf("arn:aws:ec2:%s:%s:instance/%s", region, accountId, *instance.InstanceId)
234-
resources = append(resources, api.AgentResource{
234+
resources = append(resources, api.CreateResource{
235235
Version: "compute/v1",
236236
Kind: "Instance",
237237
Name: name,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func runSync(regions *[]string, name *string) func(cmd *cobra.Command, args []st
6363
log.Info("Syncing EKS clusters", "regions", regionsToSync)
6464

6565
// Process each region
66-
var allResources []api.AgentResource
66+
var allResources []api.CreateResource
6767
var mu sync.Mutex
6868
var wg sync.WaitGroup
6969
var syncErrors []error
@@ -153,8 +153,8 @@ func initEKSClient(ctx context.Context, region string) (*eks.Client, aws.Config,
153153
return eks.NewFromConfig(cfg), cfg, nil
154154
}
155155

156-
func processClusters(ctx context.Context, eksClient *eks.Client, region string, cfg aws.Config) ([]api.AgentResource, error) {
157-
var resources []api.AgentResource
156+
func processClusters(ctx context.Context, eksClient *eks.Client, region string, cfg aws.Config) ([]api.CreateResource, error) {
157+
var resources []api.CreateResource
158158
var nextToken *string
159159

160160
accountID, err := common.GetAccountID(ctx, cfg)
@@ -197,7 +197,7 @@ func processClusters(ctx context.Context, eksClient *eks.Client, region string,
197197
return resources, nil
198198
}
199199

200-
func processCluster(_ context.Context, cluster *types.Cluster, region string, accountID string) (api.AgentResource, error) {
200+
func processCluster(_ context.Context, cluster *types.Cluster, region string, accountID string) (api.CreateResource, error) {
201201
metadata := initClusterMetadata(cluster, region)
202202

203203
metadata["aws/account"] = accountID
@@ -206,7 +206,7 @@ func processCluster(_ context.Context, cluster *types.Cluster, region string, ac
206206
region, region, *cluster.Name)
207207
metadata["ctrlplane/links"] = fmt.Sprintf("{ \"AWS Console\": \"%s\" }", consoleUrl)
208208

209-
return api.AgentResource{
209+
return api.CreateResource{
210210
Version: "ctrlplane.dev/kubernetes/cluster/v1",
211211
Kind: "AmazonElasticKubernetesService",
212212
Name: *cluster.Name,
@@ -320,7 +320,7 @@ var relationshipRules = []api.CreateResourceRelationshipRule{
320320
},
321321
}
322322

323-
func upsertToCtrlplane(ctx context.Context, resources []api.AgentResource, region, name *string) error {
323+
func upsertToCtrlplane(ctx context.Context, resources []api.CreateResource, region, name *string) error {
324324
if *name == "" {
325325
*name = fmt.Sprintf("aws-eks-%s", *region)
326326
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func runSync(regions *[]string, name *string) func(cmd *cobra.Command, args []st
6161
log.Info("Syncing RDS instances", "regions", regionsToSync)
6262

6363
// Process each region
64-
var allResources []api.AgentResource
64+
var allResources []api.CreateResource
6565
var mu sync.Mutex
6666
var wg sync.WaitGroup
6767
var syncErrors []error
@@ -151,8 +151,8 @@ func initRDSClient(ctx context.Context, region string) (*rds.Client, error) {
151151
return rds.NewFromConfig(cfg), nil
152152
}
153153

154-
func processInstances(ctx context.Context, rdsClient *rds.Client, region string) ([]api.AgentResource, error) {
155-
var resources []api.AgentResource
154+
func processInstances(ctx context.Context, rdsClient *rds.Client, region string) ([]api.CreateResource, error) {
155+
var resources []api.CreateResource
156156
var marker *string
157157

158158
for {
@@ -182,7 +182,7 @@ func processInstances(ctx context.Context, rdsClient *rds.Client, region string)
182182
return resources, nil
183183
}
184184

185-
func processInstance(ctx context.Context, instance *types.DBInstance, region string, rdsClient *rds.Client) (api.AgentResource, error) {
185+
func processInstance(ctx context.Context, instance *types.DBInstance, region string, rdsClient *rds.Client) (api.CreateResource, error) {
186186
// Get default port based on engine
187187
port := int32(5432) // Default to PostgreSQL port
188188
if instance.Endpoint != nil && instance.Endpoint.Port != nil && *instance.Endpoint.Port != 0 {
@@ -218,7 +218,7 @@ func processInstance(ctx context.Context, instance *types.DBInstance, region str
218218
}
219219
}
220220

221-
return api.AgentResource{
221+
return api.CreateResource{
222222
Version: "ctrlplane.dev/database/v1",
223223
Kind: "AmazonRelationalDatabaseService",
224224
Name: *instance.DBInstanceIdentifier,
@@ -511,7 +511,7 @@ func fetchParameterGroupDetails(ctx context.Context, rdsClient *rds.Client, para
511511
}
512512

513513
// upsertToCtrlplane handles upserting resources to Ctrlplane
514-
func upsertToCtrlplane(ctx context.Context, resources []api.AgentResource, region, name *string) error {
514+
func upsertToCtrlplane(ctx context.Context, resources []api.CreateResource, region, name *string) error {
515515
if *name == "" {
516516
*name = fmt.Sprintf("aws-rds-%s", *region)
517517
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ func getDefaultSubscriptionID(ctx context.Context, cred azcore.TokenCredential)
159159
return "", fmt.Errorf("no subscriptions found")
160160
}
161161

162-
func processClusters(ctx context.Context, cred azcore.TokenCredential, subscriptionID string, tenantID string) ([]api.AgentResource, error) {
163-
var resources []api.AgentResource
162+
func processClusters(ctx context.Context, cred azcore.TokenCredential, subscriptionID string, tenantID string) ([]api.CreateResource, error) {
163+
var resources []api.CreateResource
164164
var mu sync.Mutex
165165
var wg sync.WaitGroup
166166
var syncErrors []error
@@ -211,7 +211,7 @@ func processClusters(ctx context.Context, cred azcore.TokenCredential, subscript
211211
return resources, nil
212212
}
213213

214-
func processCluster(_ context.Context, cluster *armcontainerservice.ManagedCluster, subscriptionID string, tenantID string) (api.AgentResource, error) {
214+
func processCluster(_ context.Context, cluster *armcontainerservice.ManagedCluster, subscriptionID string, tenantID string) (api.CreateResource, error) {
215215
resourceGroup := extractResourceGroupFromID(*cluster.ID)
216216
metadata := initClusterMetadata(cluster, subscriptionID, resourceGroup, tenantID)
217217

@@ -229,7 +229,7 @@ func processCluster(_ context.Context, cluster *armcontainerservice.ManagedClust
229229
endpoint = *cluster.Properties.Fqdn
230230
}
231231

232-
return api.AgentResource{
232+
return api.CreateResource{
233233
Version: "ctrlplane.dev/kubernetes/cluster/v1",
234234
Kind: "AzureKubernetesService",
235235
Name: *cluster.Name,
@@ -425,7 +425,7 @@ var relationshipRules = []api.CreateResourceRelationshipRule{
425425
},
426426
}
427427

428-
func upsertToCtrlplane(ctx context.Context, resources []api.AgentResource, subscriptionID, name *string) error {
428+
func upsertToCtrlplane(ctx context.Context, resources []api.CreateResource, subscriptionID, name *string) error {
429429
if *name == "" {
430430
*name = fmt.Sprintf("azure-aks-%s", *subscriptionID)
431431
}

cmd/ctrlc/root/sync/clickhouse/clickhouse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func NewSyncClickhouseCmd() *cobra.Command {
223223
return fmt.Errorf("failed to list ClickHouse services: %w", err)
224224
}
225225

226-
resources := []api.AgentResource{}
226+
resources := []api.CreateResource{}
227227
for _, service := range services {
228228
var endpoints []string
229229
for _, endpoint := range service.Endpoints {
@@ -272,7 +272,7 @@ func NewSyncClickhouseCmd() *cobra.Command {
272272

273273
// Create a sanitized name
274274
name := strings.Split(service.Name, ".")[0]
275-
resources = append(resources, api.AgentResource{
275+
resources = append(resources, api.CreateResource{
276276
Version: "ctrlplane.dev/database/v1",
277277
Kind: "ClickhouseCloud",
278278
Name: name,

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func initGitHubClient(ctx context.Context, token string) (*github.Client, error)
179179
}
180180

181181
// processPullRequests lists and processes all pull requests
182-
func processPullRequests(ctx context.Context, client *github.Client, owner, repo string, states []string) ([]api.AgentResource, error) {
182+
func processPullRequests(ctx context.Context, client *github.Client, owner, repo string, states []string) ([]api.CreateResource, error) {
183183
log.Debug("Processing pull requests", "owner", owner, "repo", repo, "states", states)
184184

185185
// If no states specified or "all" is specified, include everything
@@ -270,7 +270,7 @@ func processPullRequests(ctx context.Context, client *github.Client, owner, repo
270270
"filtered", len(filteredPRs),
271271
"states", states)
272272

273-
resources := []api.AgentResource{}
273+
resources := []api.CreateResource{}
274274
for _, pr := range filteredPRs {
275275
log.Info("Processing pull request", "number", pr.GetNumber(), "source", pr.GetHead().GetRef(), "target", pr.GetBase().GetRef())
276276
resource, err := processPullRequest(ctx, client, owner, repo, pr)
@@ -454,7 +454,7 @@ func getNormalizedStatus(pr *github.PullRequest) string {
454454
}
455455

456456
// processPullRequest handles processing of a single pull request
457-
func processPullRequest(ctx context.Context, client *github.Client, owner, repo string, pr *github.PullRequest) (api.AgentResource, error) {
457+
func processPullRequest(ctx context.Context, client *github.Client, owner, repo string, pr *github.PullRequest) (api.CreateResource, error) {
458458
prNumber := pr.GetNumber()
459459
log.Debug("Processing pull request", "number", prNumber, "title", pr.GetTitle())
460460

@@ -498,7 +498,7 @@ func processPullRequest(ctx context.Context, client *github.Client, owner, repo
498498
resourceName := fmt.Sprintf("%s-%s-%d", owner, repo, prNumber)
499499
log.Debug("Creating resource", "number", prNumber, "name", resourceName)
500500

501-
return api.AgentResource{
501+
return api.CreateResource{
502502
Version: "ctrlplane.dev/git/pull-request/v1",
503503
Kind: "GitHubPullRequest",
504504
Name: resourceName,
@@ -608,7 +608,7 @@ func initPullRequestMetadata(pr *github.PullRequest, owner, repo string) map[str
608608
var relationshipRules = []api.CreateResourceRelationshipRule{}
609609

610610
// upsertToCtrlplane handles upserting resources to Ctrlplane
611-
func upsertToCtrlplane(ctx context.Context, resources []api.AgentResource, owner, repo, name string) error {
611+
func upsertToCtrlplane(ctx context.Context, resources []api.CreateResource, owner, repo, name string) error {
612612
log.Debug("Upserting resources to Ctrlplane", "count", len(resources))
613613

614614
if name == "" {

cmd/ctrlc/root/sync/google/bigtable/bigtable.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func initBigtableClient(ctx context.Context) (*bigtableadmin.Service, error) {
101101
}
102102

103103
// processInstances lists and processes all Bigtable instances
104-
func processInstances(ctx context.Context, adminClient *bigtableadmin.Service, project string) ([]api.AgentResource, error) {
104+
func processInstances(ctx context.Context, adminClient *bigtableadmin.Service, project string) ([]api.CreateResource, error) {
105105
projectParent := fmt.Sprintf("projects/%s", project)
106106
instances, err := adminClient.Projects.Instances.List(projectParent).Do()
107107
if err != nil {
@@ -110,7 +110,7 @@ func processInstances(ctx context.Context, adminClient *bigtableadmin.Service, p
110110

111111
log.Info("Found instances", "count", len(instances.Instances))
112112

113-
resources := []api.AgentResource{}
113+
resources := []api.CreateResource{}
114114
for _, instance := range instances.Instances {
115115
resource, err := processInstance(ctx, adminClient, instance, project)
116116
if err != nil {
@@ -124,7 +124,7 @@ func processInstances(ctx context.Context, adminClient *bigtableadmin.Service, p
124124
}
125125

126126
// processInstance handles processing of a single Bigtable instance
127-
func processInstance(_ context.Context, adminClient *bigtableadmin.Service, instance *bigtableadmin.Instance, project string) (api.AgentResource, error) {
127+
func processInstance(_ context.Context, adminClient *bigtableadmin.Service, instance *bigtableadmin.Instance, project string) (api.CreateResource, error) {
128128
metadata := initInstanceMetadata(instance, project)
129129

130130
// Process clusters
@@ -146,7 +146,7 @@ func processInstance(_ context.Context, adminClient *bigtableadmin.Service, inst
146146
metadata["ctrlplane/links"] = fmt.Sprintf("{ \"Google Cloud Console\": \"%s\" }", consoleUrl)
147147
instanceFullName := fmt.Sprintf("projects/%s/instances/%s", project, instance.Name)
148148

149-
return api.AgentResource{
149+
return api.CreateResource{
150150
Version: "ctrlplane.dev/database/v1",
151151
Kind: "GoogleBigtable",
152152
Name: instance.DisplayName,
@@ -292,7 +292,7 @@ func processTables(adminClient *bigtableadmin.Service, instance *bigtableadmin.I
292292
}
293293

294294
// upsertToCtrlplane handles upserting resources to Ctrlplane
295-
func upsertToCtrlplane(ctx context.Context, resources []api.AgentResource, project, name *string) error {
295+
func upsertToCtrlplane(ctx context.Context, resources []api.CreateResource, project, name *string) error {
296296
if *name == "" {
297297
*name = fmt.Sprintf("google-bigtable-project-%s", *project)
298298
}

0 commit comments

Comments
 (0)