-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfirebase.go
145 lines (122 loc) · 4.59 KB
/
firebase.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package gcputil
import (
"context"
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"google.golang.org/api/option"
"google.golang.org/api/option/internaloption"
htransport "google.golang.org/api/transport/http"
"io"
"net/http"
)
const basePath = "https://firebasedatabase.googleapis.com/"
const basePathTemplate = "https://firebasedatabase.UNIVERSE_DOMAIN/"
const mtlsBasePath = "https://firebasedatabase.mtls.googleapis.com/"
// DatabaseInstance represents a Firebase Realtime Database instance
type DatabaseInstance struct {
Name string `json:"name"`
Project string `json:"project"`
DatabaseURL string `json:"databaseUrl"`
Type string `json:"type"`
State string `json:"state"`
}
// NewFirebaseDatabaseService creates a new Firebase Realtime Database client to interact with the
// https://firebasedatabase.googleapis.com endpoints as there is no official golang client library
func NewFirebaseDatabaseService(ctx context.Context, opts ...option.ClientOption) (*FirebaseDatabaseService, error) {
scopesOption := internaloption.WithDefaultScopes(
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/cloud-platform.read-only",
"https://www.googleapis.com/auth/firebase",
"https://www.googleapis.com/auth/firebase.readonly",
)
// NOTE: prepend, so we don't override user-specified scopes.
opts = append([]option.ClientOption{scopesOption}, opts...)
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(mtlsBasePath))
opts = append(opts, internaloption.EnableNewAuthLibrary())
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
}
s := &FirebaseDatabaseService{client: client, BasePath: basePath}
if endpoint != "" {
s.BasePath = endpoint
}
return s, nil
}
type FirebaseDatabaseService struct {
client *http.Client
BasePath string // API endpoint base URL
UserAgent string // optional additional User-Agent fragment
}
// ListDatabaseRegions lists Firebase Realtime Database regions
func (s *FirebaseDatabaseService) ListDatabaseRegions() []string {
return []string{
"us-central1",
"europe-west1",
"asia-southeast1",
}
}
// ListDatabaseInstances lists Firebase Realtime Database instances
func (s *FirebaseDatabaseService) ListDatabaseInstances(ctx context.Context, parent string) ([]*DatabaseInstance, error) {
url1 := fmt.Sprintf("%sv1beta/%s/instances", s.BasePath, parent)
logrus.Tracef("url: %s", url1)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url1, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error requesting database instances: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
d, _ := io.ReadAll(resp.Body)
fmt.Println(string(d))
return nil, fmt.Errorf("API request error: status %d", resp.StatusCode)
}
var instances struct {
Instances []*DatabaseInstance `json:"instances"`
}
if err = json.NewDecoder(resp.Body).Decode(&instances); err != nil {
return nil, fmt.Errorf("error decoding response: %v", err)
}
return instances.Instances, nil
}
// DeleteDatabaseInstance deletes a Firebase Realtime Database instance
func (s *FirebaseDatabaseService) DeleteDatabaseInstance(ctx context.Context, parent, name string) error {
url := fmt.Sprintf("%sv1beta/%s/instances/%s", s.BasePath, parent, name)
logrus.Tracef("url: %s", url)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return fmt.Errorf("error deleting database instance: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API request error: status %d", resp.StatusCode)
}
return nil
}
// DisableDatabaseInstance disables a Firebase Realtime Database instance
func (s *FirebaseDatabaseService) DisableDatabaseInstance(ctx context.Context, parent, name string) error {
url := fmt.Sprintf("%sv1beta/%s/instances/%s:disable", s.BasePath, parent, name)
logrus.Tracef("url: %s", url)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return fmt.Errorf("error disabling database instance: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API request error: status %d", resp.StatusCode)
}
return nil
}