-
Notifications
You must be signed in to change notification settings - Fork 380
feat: Add ansible-inventory credential support for save command #2925
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
fullstopdev
wants to merge
6
commits into
srl-labs:main
Choose a base branch
from
fullstopdev:feat/clab-save-creds-load-2924
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a26acb
feat: Add ansible-inventory credential support for save command
c656933
Merge branch 'srl-labs:main' into feat/clab-save-creds-load-2924
fullstopdev b3049e4
Merge branch 'srl-labs:main' into feat/clab-save-creds-load-2924
fullstopdev 37a20a6
Merge branch 'srl-labs:main' into feat/clab-save-creds-load-2924
fullstopdev 15a9148
Merge branch 'srl-labs:main' into feat/clab-save-creds-load-2924
fullstopdev cae2cb0
simplify the load creds logic and use it only for nokia_sros and noki…
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 |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright 2020 Nokia | ||
| // Licensed under the BSD 3-Clause License. | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| package core | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "gopkg.in/yaml.v2" | ||
| ) | ||
|
|
||
| // AnsibleInventoryCredentials holds the credentials read from ansible inventory. | ||
| type AnsibleInventoryCredentials struct { | ||
| Username string | ||
| Password string | ||
| } | ||
|
|
||
| // AnsibleInventoryData represents the structure of the ansible-inventory.yml file | ||
| // for the purpose of reading credentials. | ||
| type AnsibleInventoryData struct { | ||
| All struct { | ||
| Children map[string]AnsibleInventoryKind `yaml:"children"` | ||
| } `yaml:"all"` | ||
| } | ||
|
|
||
| // AnsibleInventoryKind represents a node kind in the inventory. | ||
| type AnsibleInventoryKind struct { | ||
| Vars AnsibleInventoryVars `yaml:"vars,omitempty"` | ||
| Hosts map[string]AnsibleInventoryHost `yaml:"hosts,omitempty"` | ||
| } | ||
|
|
||
| // AnsibleInventoryVars represents the vars section for a kind. | ||
| type AnsibleInventoryVars struct { | ||
| User string `yaml:"ansible_user,omitempty"` | ||
| Password string `yaml:"ansible_password,omitempty"` | ||
| } | ||
|
|
||
| // AnsibleInventoryHost represents a host entry. | ||
| type AnsibleInventoryHost struct { | ||
| Host string `yaml:"ansible_host,omitempty"` | ||
| } | ||
|
|
||
| // ReadAnsibleInventoryCredentials reads the ansible-inventory.yml file and returns | ||
| // credentials for the specified node kind. | ||
| func ReadAnsibleInventoryCredentials( | ||
| inventoryPath, nodeKind string, | ||
| ) (*AnsibleInventoryCredentials, error) { | ||
| // Read the inventory file | ||
| data, err := os.ReadFile(inventoryPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read ansible inventory file: %w", err) | ||
| } | ||
|
|
||
| // Parse the YAML | ||
| var inventory AnsibleInventoryData | ||
| err = yaml.Unmarshal(data, &inventory) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse ansible inventory: %w", err) | ||
| } | ||
|
|
||
| // Look for the node kind in the inventory | ||
| kind, exists := inventory.All.Children[nodeKind] | ||
| if !exists { | ||
| return nil, fmt.Errorf("node kind %q not found in ansible inventory", nodeKind) | ||
| } | ||
|
|
||
| // Extract credentials | ||
| creds := &AnsibleInventoryCredentials{ | ||
| Username: kind.Vars.User, | ||
| Password: kind.Vars.Password, | ||
| } | ||
|
|
||
| // Validate that we have credentials | ||
| if creds.Username == "" && creds.Password == "" { | ||
| return nil, fmt.Errorf("no credentials found for kind %q in ansible inventory", nodeKind) | ||
| } | ||
|
|
||
| return creds, 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.
Those are not conform with the schema and should not use deprecated kinds
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 can keep only :
"nokia_sros", "nokia_srsim"