File: provider.go

package info (click to toggle)
golang-github-viant-toolbox 0.33.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,280 kB
  • sloc: makefile: 16
file content (73 lines) | stat: -rw-r--r-- 2,313 bytes parent folder | download | duplicates (2)
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
package gs

import (
	"context"
	"encoding/json"
	"github.com/viant/toolbox/cred"
	"github.com/viant/toolbox/secret"
	"github.com/viant/toolbox/storage"
	"golang.org/x/oauth2/google"
	"google.golang.org/api/option"
	"os"
)

const ProviderScheme = "gs"
const userAgent = "gcloud-golang-storage/20151204"
const DevstorageFullControlScope = "https://www.googleapis.com/auth/devstorage.full_control"
const googleStorageProjectKey = "GOOGLE_STORAGE_PROJECT"

func init() {
	storage.Registry().Registry[ProviderScheme] = serviceProvider
}

func serviceProvider(credentials string) (storage.Service, error) {
	var credentialOptions = make([]option.ClientOption, 0)
	var projectID = os.Getenv("GOOGLE_CLOUD_PROJECT")
	if credentials == "" {
		credentialOptions = append([]option.ClientOption{},
			option.WithScopes(DevstorageFullControlScope),
			option.WithUserAgent(userAgent))
	} else {
		if json.Valid([]byte(credentials)) {
			credentialOptions = append(credentialOptions, option.WithCredentialsJSON([]byte(credentials)))
		} else {
			credentialOptions = append(credentialOptions, option.WithCredentialsFile(credentials))
		}
		secretService := secret.New("", false)
		config, err := secretService.GetCredentials(credentials)
		if err != nil {
			return nil, err
		}
		projectID = config.ProjectID
	}

	if customProjectID := os.Getenv(googleStorageProjectKey); customProjectID != "" {
		projectID = customProjectID
	}
	if projectID == "" {
		if credentials, err := google.FindDefaultCredentials(context.Background(), DevstorageFullControlScope); err == nil {
			projectID = credentials.ProjectID
		}
	}
	return NewService(projectID, credentialOptions...), nil
}

func credServiceProvider(config *cred.Config) (storage.Service, error) {
	var credentialOptions = make([]option.ClientOption, 0)
	projectID := config.ProjectID

	if config.Data == "" {
		if data, err := json.Marshal(config); err == nil {
			config.Data = string(data)
		}
	}
	credentialOptions = append(credentialOptions, option.WithCredentialsJSON([]byte(config.Data)))
	return NewService(projectID, credentialOptions...), nil
}

//SetProvider set gs provider with supplied config
func SetProvider(config *cred.Config) {
	storage.Registry().Registry[ProviderScheme] = func(string) (storage.Service, error) {
		return credServiceProvider(config)
	}
}