File: from_map.go

package info (click to toggle)
golang-github-hashicorp-terraform-svchost 0.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: makefile: 5; sh: 4
file content (48 lines) | stat: -rw-r--r-- 1,554 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package auth

import (
	"github.com/zclconf/go-cty/cty"
)

// HostCredentialsFromMap converts a map of key-value pairs from a credentials
// definition provided by the user (e.g. in a config file, or via a credentials
// helper) into a HostCredentials object if possible, or returns nil if
// no credentials could be extracted from the map.
//
// This function ignores map keys it is unfamiliar with, to allow for future
// expansion of the credentials map format for new credential types.
func HostCredentialsFromMap(m map[string]interface{}) HostCredentials {
	if m == nil {
		return nil
	}
	if token, ok := m["token"].(string); ok {
		return HostCredentialsToken(token)
	}
	return nil
}

// HostCredentialsFromObject converts a cty.Value of an object type into a
// HostCredentials object if possible, or returns nil if no credentials could
// be extracted from the map.
//
// This function ignores object attributes it is unfamiliar with, to allow for
// future expansion of the credentials object structure for new credential types.
//
// If the given value is not of an object type, this function will panic.
func HostCredentialsFromObject(obj cty.Value) HostCredentials {
	if !obj.Type().HasAttribute("token") {
		return nil
	}

	tokenV := obj.GetAttr("token")
	if tokenV.IsNull() || !tokenV.IsKnown() {
		return nil
	}
	if !cty.String.Equals(tokenV.Type()) {
		// Weird, but maybe some future Terraform version accepts an object
		// here for some reason, so we'll be resilient.
		return nil
	}

	return HostCredentialsToken(tokenV.AsString())
}