-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_authentication.go
124 lines (96 loc) · 3.33 KB
/
resource_authentication.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
package main
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/pactflow/terraform/broker"
"github.com/pactflow/terraform/client"
)
func authentication() *schema.Resource {
return &schema.Resource{
Importer: &schema.ResourceImporter{State: schema.ImportStatePassthrough},
Create: authenticationCreate,
Read: authenticationRead,
Update: authenticationUpdate,
Delete: authenticationDelete,
Schema: map[string]*schema.Schema{
"github_organizations": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "The list of Github organisations allowed access to the account",
},
"google_domains": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "The list of Google organisation domains allowed access to the account",
},
},
}
}
func authenticationFromState(d *schema.ResourceData) broker.AuthenticationSettings {
settings := broker.AuthenticationSettings{
Providers: broker.AuthenticationProviders{},
}
orgs := ExpandStringSet(d.Get("github_organizations").(*schema.Set))
settings.Providers.Github.Organizations = orgs
domains := ExpandStringSet(d.Get("google_domains").(*schema.Set))
settings.Providers.Google.EmailDomains = domains
return settings
}
func authenticationState(d *schema.ResourceData, r *broker.AuthenticationSettings) error {
log.Printf("[DEBUG] setting authentication state: %v \n", r)
if len(r.Providers.Google.EmailDomains) > 0 {
if err := d.Set("google_domains", r.Providers.Google.EmailDomains); err != nil {
return fmt.Errorf("error creating key 'google_domains': %w", err)
}
}
if len(r.Providers.Github.Organizations) > 0 {
if err := d.Set("github_organizations", r.Providers.Github.Organizations); err != nil {
return fmt.Errorf("error creating key 'github_organizations': %w", err)
}
}
return nil
}
func authenticationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
authentication := authenticationFromState(d)
created, err := client.SetTenantAuthenticationSettings(authentication)
if err != nil {
return fmt.Errorf("error setting authentication: %w", err)
}
d.SetId(client.Config.BaseURL.Host)
if err = authenticationState(d, created); err != nil {
return fmt.Errorf("error setting authentication state: %w", err)
}
return nil
}
func authenticationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
authentication, err := client.ReadTenantAuthenticationSettings()
if err != nil {
return fmt.Errorf("error reading authentication settings: %w", err)
}
if err = authenticationState(d, authentication); err != nil {
return fmt.Errorf("error setting authentication settings state: %w", err)
}
return nil
}
func authenticationUpdate(d *schema.ResourceData, meta interface{}) error {
return authenticationCreate(d, meta)
}
func authenticationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
log.Println("[DEBUG] deleting (clearing) authentication settings")
_, err := client.SetTenantAuthenticationSettings(broker.AuthenticationSettings{})
if err != nil {
return fmt.Errorf("error deleting authentication: %w", err)
}
d.SetId("")
return nil
}