-
Notifications
You must be signed in to change notification settings - Fork 365
/
resource_mws_networks.go
119 lines (107 loc) · 4.1 KB
/
resource_mws_networks.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
package mws
import (
"context"
"fmt"
"log"
"time"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
// NewNetworksAPI creates MWSNetworksAPI instance from provider meta
func NewNetworksAPI(ctx context.Context, m any) NetworksAPI {
return NetworksAPI{m.(*common.DatabricksClient), ctx}
}
// NetworksAPI exposes the mws networks API
type NetworksAPI struct {
client *common.DatabricksClient
context context.Context
}
// Create creates a set of MWS Networks for the BYOVPC
func (a NetworksAPI) Create(network *Network) error {
networksAPIPath := fmt.Sprintf("/accounts/%s/networks", network.AccountID)
return a.client.Post(a.context, networksAPIPath, network, &network)
}
// Read returns the network object along with metadata and any additional errors when attaching to workspace
func (a NetworksAPI) Read(mwsAcctID, networksID string) (Network, error) {
var mwsNetwork Network
networksAPIPath := fmt.Sprintf("/accounts/%s/networks/%s", mwsAcctID, networksID)
err := a.client.Get(a.context, networksAPIPath, nil, &mwsNetwork)
return mwsNetwork, err
}
// Delete deletes the network object given a network id
func (a NetworksAPI) Delete(mwsAcctID, networksID string) error {
networksAPIPath := fmt.Sprintf("/accounts/%s/networks/%s", mwsAcctID, networksID)
if err := a.client.Delete(a.context, networksAPIPath, nil); err != nil {
return err
}
return resource.RetryContext(a.context, 60*time.Second, func() *resource.RetryError {
network, err := a.Read(mwsAcctID, networksID)
if apierr.IsMissing(err) {
log.Printf("[INFO] Network %s/%s is removed.", mwsAcctID, networksID)
return nil
}
if err != nil {
return resource.NonRetryableError(err)
}
msg := fmt.Errorf("Network %s is not removed yet. VPC Status: %s", network.NetworkName, network.VPCStatus)
log.Printf("[INFO] %s", msg)
return resource.RetryableError(msg)
})
}
// List lists all the available network objects in the mws account
func (a NetworksAPI) List(mwsAcctID string) ([]Network, error) {
var mwsNetworkList []Network
networksAPIPath := fmt.Sprintf("/accounts/%s/networks", mwsAcctID)
err := a.client.Get(a.context, networksAPIPath, nil, &mwsNetworkList)
return mwsNetworkList, err
}
func ResourceMwsNetworks() common.Resource {
s := common.StructToSchema(Network{}, func(s map[string]*schema.Schema) map[string]*schema.Schema {
s["account_id"].Sensitive = true
// nolint
s["network_name"].ValidateFunc = validation.StringLenBetween(4, 256)
s["subnet_ids"].MinItems = 2
s["security_group_ids"].MinItems = 1
s["security_group_ids"].MaxItems = 5
s["vpc_id"].ExactlyOneOf = []string{"vpc_id", "gcp_network_info"}
s["subnet_ids"].ExactlyOneOf = []string{"subnet_ids", "gcp_network_info"}
s["security_group_ids"].ExactlyOneOf = []string{"security_group_ids", "gcp_network_info"}
s["gcp_network_info"].ConflictsWith = []string{"vpc_id", "subnet_ids", "security_group_ids"}
return s
})
p := common.NewPairSeparatedID("account_id", "network_id", "/")
return common.Resource{
Schema: s,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
var network Network
common.DataToStructPointer(d, s, &network)
if err := NewNetworksAPI(ctx, c).Create(&network); err != nil {
return err
}
d.Set("network_id", network.NetworkID)
p.Pack(d)
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
accountID, networkID, err := p.Unpack(d)
if err != nil {
return err
}
network, err := NewNetworksAPI(ctx, c).Read(accountID, networkID)
if err != nil {
return err
}
return common.StructToData(network, s, d)
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
accountID, networkID, err := p.Unpack(d)
if err != nil {
return err
}
return NewNetworksAPI(ctx, c).Delete(accountID, networkID)
},
}
}