-
Notifications
You must be signed in to change notification settings - Fork 103
Add nomad_acl_bootstrap resource #553
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
Draft
Mongey
wants to merge
8
commits into
hashicorp:main
Choose a base branch
from
Mongey:cm/bootstrap
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
acc9d52
Initial support for adding the ability to ACL bootstrap a cluster wit…
lhaig 913d3dd
Add copyright header and fix function naming in ACL bootstrap resource
Mongey 0190cd0
Fix function name reference in provider registration
Mongey 4ae4b48
Add test coverage for ACL bootstrap resource
Mongey b1be080
make fmt
Mongey 8adddcd
Update error formatting
Mongey 18f8242
Improve error handling and test robustness for ACL bootstrap
Mongey 33af48a
update start-nomad script
Mongey 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
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 | ||
|---|---|---|---|---|
| @@ -0,0 +1,136 @@ | ||||
| // Copyright (c) HashiCorp, Inc. | ||||
| // SPDX-License-Identifier: MPL-2.0 | ||||
|
|
||||
| package nomad | ||||
|
|
||||
| import ( | ||||
| "fmt" | ||||
| "log" | ||||
| "strings" | ||||
| "time" | ||||
|
|
||||
| "github.com/hashicorp/nomad/api" | ||||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||
| ) | ||||
|
|
||||
| func resourceACLBootstrap() *schema.Resource { | ||||
| return &schema.Resource{ | ||||
| Create: resourceACLBootstrapCreate, | ||||
| Delete: resourceACLBootstrapDelete, | ||||
| Read: resourceACLBootstrapRead, | ||||
| // Exists: resourceACLBootstrapExists, | ||||
|
|
||||
| Schema: map[string]*schema.Schema{ | ||||
| "accessor_id": { | ||||
| Description: "Nomad-generated ID for this token.", | ||||
| Computed: true, | ||||
| Type: schema.TypeString, | ||||
| }, | ||||
| "bootstrap_token": { | ||||
| Description: "The value that grants access to Nomad.", | ||||
| Computed: true, | ||||
| Optional: true, | ||||
| Sensitive: true, | ||||
| Type: schema.TypeString, | ||||
| }, | ||||
| "type": { | ||||
| Description: "The type of token. This will always be 'management' for this resource.", | ||||
| Computed: true, | ||||
| Optional: true, | ||||
| Type: schema.TypeString, | ||||
| }, | ||||
| "roles": { | ||||
| Description: "The roles associated with this token.", | ||||
| Computed: true, | ||||
| Optional: true, | ||||
| Type: schema.TypeSet, | ||||
| Elem: &schema.Schema{ | ||||
| Type: schema.TypeString, | ||||
| }, | ||||
| }, | ||||
| "global": { | ||||
| Description: "Whether this token is global.", | ||||
| Computed: true, | ||||
| Optional: true, | ||||
| Type: schema.TypeBool, | ||||
| }, | ||||
| "create_time": { | ||||
| Description: "The time this token was created in RFC3339 formate", | ||||
| Computed: true, | ||||
| Optional: true, | ||||
| Type: schema.TypeString, | ||||
| }, | ||||
| }, | ||||
| } | ||||
| } | ||||
|
|
||||
| func resourceACLBootstrapCreate(d *schema.ResourceData, meta interface{}) error { | ||||
| providerConfig := meta.(ProviderConfig) | ||||
| client := providerConfig.client | ||||
|
|
||||
| token := api.BootstrapRequest{} | ||||
| if v, ok := d.GetOk("bootstrap_token"); ok { | ||||
| token.BootstrapSecret = v.(string) | ||||
| } | ||||
| // create our token | ||||
|
Member
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. We can leave comments like this out, especially given the log following right behind it.
Suggested change
|
||||
| log.Println("[DEBUG] Creating ACL Bootstrap token with %v", token.BootstrapSecret) | ||||
| resp, _, err := client.ACLTokens().BootstrapOpts(token.BootstrapSecret, nil) | ||||
| if err != nil { | ||||
| // Check if bootstrap was already done | ||||
| if strings.Contains(err.Error(), "ACL bootstrap already done") { | ||||
| return fmt.Errorf("ACL bootstrap has already been performed on this cluster. Cannot bootstrap again") | ||||
| } | ||||
| return fmt.Errorf("error bootstrapping the cluster: %w", err) | ||||
| } | ||||
| log.Printf("[DEBUG] Created ACL token AccessorID %q", resp.AccessorID) | ||||
| log.Printf("[DEBUG] Created ACL token %q", resp) | ||||
| d.SetId(resp.AccessorID) | ||||
| d.Set("accessor_id", resp.AccessorID) | ||||
| d.Set("bootstrap_token", resp.SecretID) | ||||
| d.Set("name", resp.Name) | ||||
| d.Set("type", resp.Type) | ||||
| d.Set("roles", resp.Roles) | ||||
| d.Set("global", resp.Global) | ||||
| d.Set("create_time", resp.CreateTime.Format(time.RFC3339)) | ||||
|
|
||||
| return nil | ||||
| } | ||||
|
|
||||
| // not implemented as a cluster bootstrap can't be reverted | ||||
| func resourceACLBootstrapDelete(d *schema.ResourceData, meta interface{}) error { | ||||
| return nil | ||||
| } | ||||
|
|
||||
| func resourceACLBootstrapRead(d *schema.ResourceData, meta interface{}) error { | ||||
| // providerConfig := meta.(ProviderConfig) | ||||
| // client := providerConfig.client | ||||
| // accessor := d.Id() | ||||
|
|
||||
| // log.Printf("[DEBUG] Reading ACL bootstrap token %q", accessor) | ||||
| // token, _, err := client.ACLTokens().Info(accessor, nil) | ||||
| // if err != nil { | ||||
| // if strings.Contains(err.Error(), "404") { | ||||
| // } | ||||
| // return fmt.Errorf("error reading ACL token %q: %s", accessor, err.Error()) | ||||
| // } | ||||
| // log.Printf("[DEBUG] Read ACL bootstrap token %q", accessor) | ||||
| // | ||||
| // d.Set("accessor_id", token.AccessorID) | ||||
| // d.Set("bootstrap_token", token.SecretID) | ||||
|
|
||||
| return nil | ||||
| } | ||||
|
|
||||
| // func resourceACLBootstrapExists(d *schema.ResourceData, meta interface{}) (bool, error) { | ||||
| // providerConfig := meta.(ProviderConfig) | ||||
| // client := providerConfig.client | ||||
| // | ||||
| // accessor := d.Id() | ||||
| // log.Printf("[DEBUG] Checking if ACL token %q exists", accessor) | ||||
| // _, _, err := client.ACLTokens().Info(accessor, nil) | ||||
| // if err != nil { | ||||
| // return true, fmt.Errorf("error checking for ACL token %q: %#v", accessor, err) | ||||
| // } | ||||
| // | ||||
| // return true, nil | ||||
| // } | ||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only support recent versions of Go, so any place we have
interface{}we can useanynow: