-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (66 loc) · 1.88 KB
/
main.go
File metadata and controls
82 lines (66 loc) · 1.88 KB
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
82
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/GermanVor/datasphere-cleaning-script/cleaningScript"
"github.com/GermanVor/datasphere-cleaning-script/common"
"github.com/GermanVor/datasphere-cleaning-script/service/datasphere"
"github.com/joho/godotenv"
)
// Take it from https://cloud.yandex.ru/docs/iam/operations/iam-token/create
var AUTH_TOKEN = ""
var ORGANIZATION_ID = ""
var AUTHORIZATION_TOKEN string
var COMMUNITY_SUBSTR = ""
func init() {
godotenv.Load(".env")
if authToken, ok := os.LookupEnv("AUTH_TOKEN"); ok {
AUTH_TOKEN = authToken
} else {
log.Fatalln("there is no AUTH_TOKEN in env file.")
}
if organizationId, ok := os.LookupEnv("ORGANIZATION_ID"); ok {
ORGANIZATION_ID = organizationId
} else {
log.Fatalln("there is no ORGANIZATION_ID in env file.")
}
if communitySubstr, ok := os.LookupEnv("COMMUNITY_SUBSTR"); ok {
COMMUNITY_SUBSTR = communitySubstr
} else {
log.Fatalln("there is no ORGANIZATION_ID in env file.")
}
req, err := http.NewRequest(
"POST",
"https://iam.api.cloud-preprod.yandex.net/iam/v1/tokens",
bytes.NewBuffer([]byte(fmt.Sprintf(`{"yandexPassportOauthToken":"%s"}`, AUTH_TOKEN))),
)
common.Fatalln(err)
resp, err := http.DefaultClient.Do(req)
common.Fatalln(err)
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalln(resp)
}
body, err := io.ReadAll(resp.Body)
common.Fatalln(err)
respBody := map[string]string{}
err = json.Unmarshal(body, &respBody)
common.Fatalln(err)
if iamToken, ok := respBody["iamToken"]; ok {
AUTHORIZATION_TOKEN = fmt.Sprintf("Bearer %s", iamToken)
} else {
log.Fatalln(respBody)
}
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
datasphereClient := datasphere.InitClient(ctx, AUTHORIZATION_TOKEN)
cleaningScript.Run(datasphereClient, ORGANIZATION_ID, COMMUNITY_SUBSTR)
}