Skip to content

Commit 5a86a46

Browse files
committed
feat(resource): firebase oauth provider
1 parent 0dc1f57 commit 5a86a46

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package resources
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/gotidy/ptr"
8+
9+
"github.com/ekristen/libnuke/pkg/registry"
10+
"github.com/ekristen/libnuke/pkg/resource"
11+
"github.com/ekristen/libnuke/pkg/types"
12+
13+
"github.com/ekristen/gcp-nuke/pkg/gcputil"
14+
"github.com/ekristen/gcp-nuke/pkg/nuke"
15+
)
16+
17+
const FirebaseOAuthProviderResource = "FirebaseOAuthProvider"
18+
19+
func init() {
20+
registry.Register(&registry.Registration{
21+
Name: FirebaseOAuthProviderResource,
22+
Scope: nuke.Project,
23+
Resource: &FirebaseOAuthProvider{},
24+
Lister: &FirebaseOAuthProviderLister{},
25+
})
26+
}
27+
28+
type FirebaseOAuthProviderLister struct {
29+
svc *gcputil.IdentityPlatformService
30+
}
31+
32+
func (l *FirebaseOAuthProviderLister) List(ctx context.Context, o interface{}) ([]resource.Resource, error) {
33+
var resources []resource.Resource
34+
35+
opts := o.(*nuke.ListerOpts)
36+
if err := opts.BeforeList(nuke.Global, "identitytoolkit.googleapis.com"); err != nil {
37+
return resources, err
38+
}
39+
40+
if l.svc == nil {
41+
var err error
42+
l.svc, err = gcputil.NewIdentityPlatformService(ctx, opts.ClientOptions...)
43+
if err != nil {
44+
return nil, err
45+
}
46+
}
47+
48+
builtinProviders, err := l.svc.ListDefaultSupportedOAuthIdpConfigs(ctx, *opts.Project)
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
for _, provider := range builtinProviders.DefaultSupportedIdpConfigs {
54+
parts := strings.Split(*provider.Name, "/")
55+
shortName := parts[len(parts)-1]
56+
57+
resources = append(resources, &FirebaseOAuthProvider{
58+
svc: l.svc,
59+
project: opts.Project,
60+
Name: ptr.String(shortName),
61+
Type: ptr.String("builtin"),
62+
})
63+
}
64+
65+
customProviders, err := l.svc.ListOAuthIdpConfigs(ctx, *opts.Project)
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
for _, provider := range customProviders.OAuthIdpConfigs {
71+
parts := strings.Split(*provider.Name, "/")
72+
shortName := parts[len(parts)-1]
73+
74+
resources = append(resources, &FirebaseOAuthProvider{
75+
svc: l.svc,
76+
project: opts.Project,
77+
Name: ptr.String(shortName),
78+
Type: ptr.String("custom"),
79+
})
80+
}
81+
82+
return resources, nil
83+
}
84+
85+
type FirebaseOAuthProvider struct {
86+
svc *gcputil.IdentityPlatformService
87+
project *string
88+
Name *string `description:"The name of the OAuth provider"`
89+
Type *string `description:"The type of the OAuth provider, either builtin or custom"`
90+
}
91+
92+
func (r *FirebaseOAuthProvider) Remove(ctx context.Context) error {
93+
if ptr.ToString(r.Type) == "builtin" {
94+
return r.svc.DeleteDefaultSupportedOAuthIdpConfig(ctx, *r.project, *r.Name)
95+
}
96+
97+
return r.svc.DeleteOAuthIdpConfig(ctx, *r.project, *r.Name)
98+
}
99+
100+
func (r *FirebaseOAuthProvider) Properties() types.Properties {
101+
return types.NewPropertiesFromStruct(r)
102+
}
103+
104+
func (r *FirebaseOAuthProvider) String() string {
105+
return *r.Name
106+
}

0 commit comments

Comments
 (0)