-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
81 lines (65 loc) · 2.01 KB
/
main.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"fmt"
"log"
"os"
"go.mongodb.org/atlas-sdk/v20231115005/admin"
)
func main() {
ctx := context.Background()
public_key := os.Getenv("ATLAS_PUBLIC_KEY")
private_key := os.Getenv("ATLAS_PRIVATE_KEY")
sdk, err := admin.NewClient(admin.UseDigestAuth(public_key, private_key))
if err != nil {
log.Fatalf("Error when instantiating new client: %v", err)
}
projects, _, err := sdk.ProjectsApi.ListProjects(ctx).Execute()
if err != nil {
log.Fatalf("Could not fetch projects: %v", err)
}
for _, project := range *projects.Results {
clusters, _, err := sdk.ClustersApi.ListClusters(ctx, *project.Id).Execute()
if err != nil {
log.Fatalf("Could not fetch clusters: %v", err)
}
for _, cluster := range *clusters.Results {
fmt.Println("CLUSTER:", *cluster.Name)
if labels := cluster.GetLabels(); labels != nil && len(labels) > 0 {
fmt.Println("LABELS:")
for _, label := range labels {
fmt.Printf(" %s: %s\n", label.GetKey(), label.GetValue())
}
if tags := cluster.GetTags(); tags == nil || len(tags) == 0 {
fmt.Println("Creating tags from labels...")
newResourceTags := make([]admin.ResourceTag, 0, len(labels))
for _, label := range labels {
tag := admin.NewResourceTag()
key := label.GetKey()
value := label.GetValue()
tag.Key = &key
tag.Value = &value
newResourceTags = append(newResourceTags, *tag)
}
cluster.SetTags(newResourceTags)
fmt.Println("Valida tags")
for _, tag := range cluster.GetTags() {
fmt.Printf(" %s: %s\n", *tag.Key, *tag.Value)
}
cluster.ConnectionStrings = nil
_, _, err := sdk.ClustersApi.UpdateCluster(ctx, *project.Id, *cluster.Name, &cluster).Execute()
if err != nil {
fmt.Println("Could not update cluster: %v", err)
continue
}
fmt.Println("New TAGS:")
for _, tag := range newResourceTags {
fmt.Printf(" %s: %s\n", *tag.Key, *tag.Value)
}
}
} else {
fmt.Println("LABELS: None")
}
}
}
}