utils

package
v5.0.1-rc2+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const CustomUserAgent = "cf-gcp-service-broker " + Version

This custom user agent string is added to provision calls so that Google can track the aggregated use of this tool We can better advocate for devoting resources to supporting cloud foundry and this service broker if we can show good usage statistics for it, so if you feel the need to fork this repo, please leave this string in place!

View Source
const (
	EnvironmentVarPrefix = "gsb"
)
View Source
const Version = "5.0.1"

Version sets the version for the whole GCP service broker software.

Variables

View Source
var (
	PropertyToEnvReplacer = strings.NewReplacer(".", "_", "-", "_")
)

Functions

func CopyStringMap

func CopyStringMap(m map[string]string) map[string]string

CopyStringMap makes a copy of the given map.

Example
m := map[string]string{"a": "one"}
copy := CopyStringMap(m)
m["a"] = "two"

fmt.Println(m["a"])
fmt.Println(copy["a"])
Output:

two
one

func ExtractDefaultLabels

func ExtractDefaultLabels(instanceId string, details brokerapi.ProvisionDetails) map[string]string

ExtractDefaultLabels creates a map[string]string of labels that should be applied to a resource on creation if the resource supports labels. These include the organization, space, and instance id.

func GetAuthedConfig

func GetAuthedConfig() (*jwt.Config, error)

func GetDefaultProjectId

func GetDefaultProjectId() (string, error)

GetDefaultProject gets the default project id for the service broker based on the JSON Service Account key.

Example
serviceAccountJson := `{
	  "//": "Dummy account from https://github.com/GoogleCloudPlatform/google-cloud-java/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java",
	  "private_key_id": "somekeyid",
	  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC+K2hSuFpAdrJI\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHgaR\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\nQP/9dJfIkIDJ9Fw9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j5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa2AY7eafmoU/nZPT\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\nJ7gSidI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\nEfeFCoOX75MxKwXs6xgrw4W//AYGGUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKkXyRDW4IG1Oa2p\nrALStNBx5Y9t0/LQnFI4w3aG\n-----END PRIVATE KEY-----\n",
	  "client_email": "someclientid@developer.gserviceaccount.com",
	  "client_id": "someclientid.apps.googleusercontent.com",
	  "type": "service_account",
	  "project_id": "my-project-123"
	}`

os.Setenv("ROOT_SERVICE_ACCOUNT_JSON", serviceAccountJson)
defer os.Unsetenv("ROOT_SERVICE_ACCOUNT_JSON")

projectId, err := GetDefaultProjectId()
fmt.Printf("%s, %v\n", projectId, err)
Output:

my-project-123, <nil>

func GetServiceAccountJson

func GetServiceAccountJson() string

GetServiceAccountJson gets the raw JSON credentials of the Service Account the service broker acts as.

func Indent

func Indent(text, by string) string

Indent indents every line of the given text with the given string.

Example
weirdText := "First\n\tSecond"
out := Indent(weirdText, "  ")
fmt.Println(out == "  First\n  \tSecond")
Output:

true

func NewLogger

func NewLogger(name string) lager.Logger

NewLogger creates a new lager.Logger with the given name that has correct writing settings.

func PrettyPrintOrErr

func PrettyPrintOrErr(content interface{}) error

PrettyPrintOrErr writes a JSON serialized version of the content to stdout. If a failure occurs during marshaling, the error is logged along with a formatted version of the object and the function will return the error.

func PrettyPrintOrExit

func PrettyPrintOrExit(content interface{})

PrettyPrintOrExit writes a JSON serialized version of the content to stdout. If a failure occurs during marshaling, the error is logged along with a formatted version of the object and the program exits with a failure status.

func PropertyToEnv

func PropertyToEnv(propertyName string) string

PropertyToEnv converts a Viper configuration property name into an environment variable prefixed with EnvironmentVarPrefix

Example
env := PropertyToEnv("my.property.key-value")
fmt.Println(env)
Output:

GSB_MY_PROPERTY_KEY_VALUE

func PropertyToEnvUnprefixed

func PropertyToEnvUnprefixed(propertyName string) string

PropertyToEnvUnprefixed converts a Viper configuration property name into an environment variable using PropertyToEnvReplacer

Example
env := PropertyToEnvUnprefixed("my.property.key-value")
fmt.Println(env)
Output:

MY_PROPERTY_KEY_VALUE

func SetParameter

func SetParameter(input json.RawMessage, key string, value interface{}) (json.RawMessage, error)

SetParameter sets a value on a JSON raw message and returns a modified version with the value set

Example
// Creates an object if none is input
out, err := SetParameter(nil, "foo", 42)
fmt.Printf("%s, %v\n", string(out), err)

// Replaces existing values
out, err = SetParameter([]byte(`{"replace": "old"}`), "replace", "new")
fmt.Printf("%s, %v\n", string(out), err)
Output:

{"foo":42}, <nil>
{"replace":"new"}, <nil>

func SingleLineErrorFormatter

func SingleLineErrorFormatter(es []error) string

SingleLineErrorFormatter creates a single line error string from an array of errors.

func SplitNewlineDelimitedList

func SplitNewlineDelimitedList(paksText string) []string

SplitNewlineDelimitedList splits a list of newline delimited items and trims any leading or trailing whitespace from them.

func UnmarshalObjectRemainder

func UnmarshalObjectRemainder(data []byte, v interface{}) ([]byte, error)

UnmarshalObjectRemaidner unmarshals an object into v and returns the remaining key/value pairs as a JSON string by doing a set difference.

Example
var obj struct {
	A string `json:"a_str"`
	B int
}

remainder, err := UnmarshalObjectRemainder([]byte(`{"a_str":"hello", "B": 33, "C": 123}`), &obj)
fmt.Printf("%s, %v\n", string(remainder), err)

remainder, err = UnmarshalObjectRemainder([]byte(`{"a_str":"hello", "B": 33}`), &obj)
fmt.Printf("%s, %v\n", string(remainder), err)
Output:

{"C":123}, <nil>
{}, <nil>

Types

type StringSet

type StringSet map[string]bool

StringSet is a set data structure for strings

func NewStringSet

func NewStringSet(contents ...string) StringSet

NewStringSet creates a new string set with the given contents.

Example
a := NewStringSet()
a.Add("a")
a.Add("b")

b := NewStringSet("b", "a")

fmt.Println(a.Equals(b))
Output:

true

func NewStringSetFromStringMapKeys

func NewStringSetFromStringMapKeys(contents map[string]string) StringSet

NewStringSet creates a new string set with the given contents.

Example
m := map[string]string{
	"a": "some a value",
	"b": "some b value",
}

set := NewStringSetFromStringMapKeys(m)
fmt.Println(set)
Output:

[a b]

func (StringSet) Add

func (set StringSet) Add(str ...string)

Add puts one or more elements into the set.

Example
set := NewStringSet()
set.Add("a")
set.Add("b")

fmt.Println(set)
set.Add("a")
fmt.Println(set)
Output:

[a b]
[a b]

func (StringSet) Contains

func (set StringSet) Contains(other string) bool

Contains performs a set membership check.

Example
a := NewStringSet("a", "b")
fmt.Println(a.Contains("z"))
fmt.Println(a.Contains("a"))
Output:

false
true

func (StringSet) Equals

func (set StringSet) Equals(other StringSet) bool

Equals compares the contents of the two sets and returns true if they are the same.

Example
a := NewStringSet("a", "b")
b := NewStringSet("a", "b", "c")
fmt.Println(a.Equals(b))

a.Add("c")
fmt.Println(a.Equals(b))
Output:

false
true

func (StringSet) IsEmpty

func (set StringSet) IsEmpty() bool

IsEmpty determines if the set has zero elements.

Example
a := NewStringSet()

fmt.Println(a.IsEmpty())
a.Add("a")
fmt.Println(a.IsEmpty())
Output:

true
false

func (StringSet) Minus

func (set StringSet) Minus(other StringSet) StringSet

Returns a copy of this set with every string in the other removed.

Example
a := NewStringSet("a", "b")
b := NewStringSet("b", "c")
delta := a.Minus(b)

fmt.Println(delta)
Output:

[a]

func (StringSet) String

func (set StringSet) String() string

String converts this set to a human readable string.

func (StringSet) ToSlice

func (set StringSet) ToSlice() []string

ToSlice converts the set to a slice with sort.Strings order.

Example
a := NewStringSet()
a.Add("z")
a.Add("b")

fmt.Println(a.ToSlice())
Output:

[b z]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL