Created
June 26, 2018 01:36
-
-
Save DazWilkin/11e8944d4ba4052de4d55776941a7d94 to your computer and use it in GitHub Desktop.
Google Runtime Config Golang sample
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
package main | |
import ( | |
"fmt" | |
"log" | |
"math/rand" | |
"os" | |
"sync" | |
"time" | |
"golang.org/x/net/context" | |
"golang.org/x/oauth2/google" | |
"google.golang.org/api/runtimeconfig/v1beta1" | |
) | |
const ( | |
path = "path/to/variables" | |
) | |
const letters = "abcdefghijklmnopqrstuvwxyz" | |
func init() { | |
rand.Seed(time.Now().UnixNano()) | |
} | |
// Credit: https://stackoverflow.com/a/31832326/609290 | |
func randomString(n int) string { | |
b := make([]byte, n) | |
for i := range b { | |
b[i] = letters[rand.Intn(len(letters))] | |
} | |
return string(b) | |
} | |
func main() { | |
// Requires GCP Project ID (PROJECT_ID) | |
// Project must have Runtime Config enabled | |
// `gcloud enable services runtimeconfig.googleapis.com --project=${PROJECT_ID}`` | |
projectID := os.Getenv("PROJECT_ID") | |
if projectID == "" { | |
log.Fatalf("Environment variable 'PROJECT_ID' must be set") | |
} | |
// Create Client | |
ctx := context.Background() | |
// Assumes Application Default Credentials | |
// Ensure `gcloud auth application-default login` | |
// Reference a Service Account (GOOGLE_APPLICATION_CREDENTIALS) | |
// E.g. export GOOGLE_APPLICATION_CREDENTIALS=$PWD/client_secret.json | |
client, err := google.DefaultClient( | |
ctx, | |
runtimeconfig.CloudruntimeconfigScope, | |
) | |
if err != nil { | |
log.Fatalf("Unable to create client using ADCs: %v", err) | |
} | |
runtimeconfigService, err := runtimeconfig.New(client) | |
if err != nil { | |
log.Fatalf("Unable to create Runtime Config service: %v", err) | |
} | |
projectFullName := fmt.Sprintf("projects/%s", projectID) | |
// Create Config | |
configName := randomString(10) | |
configFullName := fmt.Sprintf("%s/configs/%s", projectFullName, configName) | |
log.Printf("Creating Config: %s\n", configFullName) | |
_, err = runtimeconfigService.Projects.Configs.Create( | |
projectFullName, | |
&runtimeconfig.RuntimeConfig{ | |
Name: configFullName, | |
}).Do() | |
if err != nil { | |
log.Fatalf("Unable to create Config: %v", err) | |
} | |
// List Configs | |
log.Println("Listing Configs") | |
next := "" | |
for { | |
configsList, err := runtimeconfigService.Projects.Configs.List(projectFullName).PageToken(next).Do() | |
if err != nil { | |
log.Fatalf("Unable to List Configs: %v", err) | |
} | |
for _, config := range configsList.Configs { | |
log.Println(config.Name) | |
} | |
next = configsList.NextPageToken | |
if next == "" { | |
break | |
} | |
} | |
// Asynchronously: Create Variables (under Config) | |
log.Println("Creating Variables") | |
// Coordinate go-routines | |
var wg sync.WaitGroup | |
count := 10 | |
// Add `count` variables | |
for i := 0; i < count; i++ { | |
// Increment the WaitGroup counter before each go-routine call | |
wg.Add(1) | |
// Go-routine | |
go func( | |
parent string, | |
variable struct { | |
key string | |
text string | |
}) { | |
// Ensure WaitGroup counter is decremented when go-routine completes | |
defer wg.Done() | |
log.Printf("Creating Variable %s=%s", variable.key, variable.text) | |
_, err = runtimeconfigService.Projects.Configs.Variables.Create( | |
configFullName, | |
&runtimeconfig.Variable{ | |
Name: fmt.Sprintf("%s/variables/%s", configFullName, variable.key), | |
Text: variable.text, | |
}).Do() | |
if err != nil { | |
log.Fatalf("Unable to create variable %s: %s", variable.key, err) | |
} | |
}(configFullName, struct { | |
key string | |
text string | |
}{ | |
// Runtime Config permits hierarchical variable namespace | |
// But values are only permitted at leaves | |
// Create: | |
// path/to/variables/A | |
// path/to/variables/B | |
// But path/C, path/to/C would not be permitted | |
key: fmt.Sprintf("%s/%s", path, randomString(10)), | |
text: randomString(10), | |
}) | |
} | |
// Wait for all go-routines to complete | |
wg.Wait() | |
// List Variables under Config | |
log.Println("List Variables") | |
next = "" | |
for { | |
variablesList, err := runtimeconfigService.Projects.Configs.Variables.List(configFullName).PageToken(next).Do() | |
if err != nil { | |
log.Fatalf("Unable to List Variables: %v", err) | |
} | |
for _, variable := range variablesList.Variables { | |
log.Printf("%s: %s", variable.Name, variable.Text) | |
} | |
next = variablesList.NextPageToken | |
if next == "" { | |
break | |
} | |
} | |
// Delete Config(s) | |
log.Println("Delete Config") | |
_, err = runtimeconfigService.Projects.Configs.Delete(configFullName).Do() | |
if err != nil { | |
log.Fatalf("Unable to Delete Config: %v", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment