-
Notifications
You must be signed in to change notification settings - Fork 1
feat: support VPC network type and existing network attachment for instance creation #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cokerrd
wants to merge
4
commits into
zsoftly:main
Choose a base branch
from
cokerrd:feat/instance-create-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f85bfad
test: add instance create test
cokerrd af96d2b
feat: instance creation with VPC network type and attach existing ne…
cokerrd b576ffd
fix: bad request on instance creation with new network
cokerrd b19f625
feat: validate --default-network against --networks specified
cokerrd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "slices" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
@@ -18,6 +19,12 @@ import ( | |
| "github.com/zsoftly/zcp-cli/pkg/api/instance" | ||
| ) | ||
|
|
||
| const ( | ||
| NetworkTypeL2 = "L2" | ||
| NetworkTypeIsolated = "Isolated" | ||
| NetworkTypeVpc = "Vpc" | ||
| ) | ||
|
|
||
| // instanceGetRetryWait controls the backoff between transient-routing-error retries. | ||
| // Overridden in tests to avoid real sleeps. | ||
| var instanceGetRetryWait = func(attempt int) time.Duration { | ||
|
|
@@ -368,8 +375,17 @@ func newInstanceCreateCmd() *cobra.Command { | |
| disk int | ||
| wait bool | ||
| isPublic bool | ||
| networks []string | ||
| vrPlan string | ||
| defaultNetwork string | ||
| ) | ||
|
|
||
| var validNetworkTypes = map[string]bool{ | ||
| NetworkTypeL2: true, | ||
| NetworkTypeIsolated: true, | ||
| NetworkTypeVpc: true, | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "create", | ||
| Short: "Create a new virtual machine", | ||
|
|
@@ -404,9 +420,6 @@ func newInstanceCreateCmd() *cobra.Command { | |
| if storageCategory == "" { | ||
| return fmt.Errorf("--storage-category is required") | ||
| } | ||
| if networkPlan == "" { | ||
| return fmt.Errorf("--network-plan is required") | ||
| } | ||
| if userData != "" && userDataFile != "" { | ||
| return fmt.Errorf("--user-data and --user-data-file are mutually exclusive") | ||
| } | ||
|
|
@@ -417,9 +430,40 @@ func newInstanceCreateCmd() *cobra.Command { | |
| } | ||
| userData = string(data) | ||
| } | ||
|
|
||
| if networkType == "L2" && isPublic { | ||
| return fmt.Errorf("--is-public cannot be true for L2 networks; pass --is-public=false") | ||
| if !validNetworkTypes[networkType] { | ||
| return fmt.Errorf("invalid value %q for --network-type: must be one of L2, Isolated, Vpc", networkType) | ||
| } | ||
| switch networkType { | ||
| case NetworkTypeL2: | ||
| if networkPlan == "" && len(networks) == 0 { | ||
| return fmt.Errorf("--network-plan or --networks is required when --network-type is '%s'", networkType) | ||
| } | ||
| if vrPlan != "" { | ||
| return fmt.Errorf("--vr-plan is not allowed when --network-type is '%s'", networkType) | ||
| } | ||
| if isPublic { | ||
| return fmt.Errorf("--is-public cannot be true for '%s' networks; pass --is-public=false", networkType) | ||
| } | ||
| case NetworkTypeIsolated: | ||
| if networkPlan == "" && len(networks) == 0 { | ||
| return fmt.Errorf("--network-plan or --networks is required when --network-type is '%s'", networkType) | ||
| } | ||
| if vrPlan != "" { | ||
| return fmt.Errorf("--vr-plan is not allowed when --network-type is '%s'", networkType) | ||
| } | ||
| case NetworkTypeVpc: | ||
| if vrPlan == "" && len(networks) == 0 { | ||
| return fmt.Errorf("--vr-plan or --networks is required when --network-type is '%s'", networkType) | ||
| } | ||
| if networkPlan != "" { | ||
| return fmt.Errorf("--network-plan is not allowed when --network-type is '%s'", networkType) | ||
| } | ||
| } | ||
| if len(networks) > 1 && defaultNetwork == "" { | ||
| return fmt.Errorf("--default-network is required when attaching multiple networks") | ||
| } | ||
| if defaultNetwork != "" && !slices.Contains(networks, defaultNetwork) { | ||
| return fmt.Errorf("--default-network must be one of --networks") | ||
| } | ||
|
|
||
| h := hostname | ||
|
|
@@ -476,7 +520,7 @@ func newInstanceCreateCmd() *cobra.Command { | |
| Template: template, | ||
| IsPublic: isPublic, | ||
| NetworkType: networkType, | ||
| Networks: []string{}, | ||
| Networks: networks, | ||
| BillingCycle: billingCycle, | ||
| SSHKey: sshKeyPtr, | ||
| AuthMethod: authMethod, | ||
|
|
@@ -491,6 +535,8 @@ func newInstanceCreateCmd() *cobra.Command { | |
| ComputeCategory: computeCategory, | ||
| BlockstoragePlan: blockstoragePlan, | ||
| NetworkPlan: networkPlan, | ||
| DefaultNetwork: defaultNetwork, | ||
| VrPlan: vrPlan, | ||
| UserData: userDataPtr, | ||
| } | ||
| return runInstanceCreate(cmd, req, wait) | ||
|
|
@@ -501,15 +547,18 @@ func newInstanceCreateCmd() *cobra.Command { | |
| cmd.Flags().StringVar(&project, "project", "", "Project slug (required)") | ||
| cmd.Flags().StringVar(®ion, "region", "", "Region slug (required)") | ||
| cmd.Flags().StringVar(&template, "template", "", "Template slug (required)") | ||
| cmd.Flags().StringVar(&plan, "plan", "", "Plan slug (required)") | ||
| cmd.Flags().StringVar(&plan, "plan", "", "Plan slug (e.g. ca2sxs- see: zcp plan vm (required)") | ||
| cmd.Flags().StringVar(&billingCycle, "billing-cycle", "", "Billing cycle slug: hourly, monthly, etc. (required)") | ||
| cmd.Flags().StringVar(&networkType, "network-type", "Isolated", "Network type (default: Isolated)") | ||
| cmd.Flags().StringVar(&networkType, "network-type", "Isolated", "Network type: Isolated, L2 or Vpc (required)") | ||
| cmd.Flags().StringVar(&sshKey, "ssh-key", "", "Name of an existing SSH key to attach for login (optional; see 'zcp ssh-key list')") | ||
| cmd.Flags().StringVar(&hostname, "hostname", "", "Hostname (defaults to --name)") | ||
| cmd.Flags().StringVar(&storageCategory, "storage-category", "", "Storage category (required, e.g. premium-ssd - see: zcp plan storage)") | ||
| cmd.Flags().StringVar(&computeCategory, "compute-category", "", "Compute category slug (optional)") | ||
| cmd.Flags().StringVar(&blockstoragePlan, "blockstorage-plan", "", "Block storage plan slug (optional, e.g. b2g1 — see: zcp plan storage)") | ||
| cmd.Flags().StringVar(&networkPlan, "network-plan", "", "Network plan slug (required, e.g. pnet-yow, pnet-yul — see: zcp plan network)") | ||
| cmd.Flags().StringVar(&networkPlan, "network-plan", "", "Network plan slug (optional; required when creating an Isolated or L2 network type— see: zcp plan network)") | ||
| cmd.Flags().StringVar(&vrPlan, "vr-plan", "", "Virtual router plan slug (optional; required when creating a VPC — see: zcp plan router)") | ||
|
Comment on lines
+550
to
+559
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Correct the conditional flag help text.
🤖 Prompt for AI Agents |
||
| cmd.Flags().StringVar(&defaultNetwork, "default-network", "", "Default network slug (optional; required when attaching multiple networks)") | ||
| cmd.Flags().StringSliceVar(&networks, "networks", []string{}, "List of network slugs to attach to the instance (optional; see: zcp network list)") | ||
| cmd.Flags().StringVar(&userData, "user-data", "", "Startup script content (cloud-init / bash)") | ||
| cmd.Flags().StringVar(&userDataFile, "user-data-file", "", "Path to a file containing the startup script") | ||
| cmd.Flags().IntVar(&cpu, "cpu", 0, "Number of vCPUs for a custom plan (e.g. 2)") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.