forked from megaport/terraform-provider-megaport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
153 lines (133 loc) · 4.64 KB
/
provider.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
146
147
148
149
150
151
152
153
// Copyright 2020 Megaport Pty Ltd
//
// Licensed under the Mozilla Public License, Version 2.0 (the
// "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://mozilla.org/MPL/2.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/megaport/terraform-provider-megaport/data_megaport"
"github.com/megaport/terraform-provider-megaport/resource_megaport"
"github.com/megaport/terraform-provider-megaport/terraform_utility"
)
const ERR_USER_NOT_ACCEPT_TOS = "sorry, you haven't accepted the Megaport terms of service"
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"environment": {
Type: schema.TypeString,
Optional: true,
Default: "staging",
DefaultFunc: schema.EnvDefaultFunc("MEGAPORT_ENVIRONMENT", nil),
},
"accept_purchase_terms": {
Type: schema.TypeBool,
Required: true,
},
"username": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("MEGAPORT_USERNAME", nil),
},
"password": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("MEGAPORT_PASSWORD", nil),
},
"mfa_otp_key": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("MEGAPORT_MFA_OTP_KEY", nil),
},
"delete_ports": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
ResourcesMap: map[string]*schema.Resource{
"megaport_port": resource_megaport.MegaportPort(),
"megaport_vxc": resource_megaport.MegaportVXC(),
"megaport_aws_connection": resource_megaport.MegaportAWSConnection(),
"megaport_mcr": resource_megaport.MegaportAWS(),
"megaport_gcp_connection": resource_megaport.MegaportGcpConnection(),
"megaport_azure_connection": resource_megaport.MegaportAzureConnection(),
"megaport_oci_connection": resource_megaport.MegaportOciConnection(),
},
ConfigureFunc: providerConfigure,
DataSourcesMap: map[string]*schema.Resource{
"megaport_port": data_megaport.MegaportPort(),
"megaport_location": data_megaport.MegaportLocation(),
"megaport_vxc": data_megaport.MegaportVXC(),
"megaport_partner_port": data_megaport.MegaportPartnerPort(),
"megaport_aws_connection": data_megaport.MegaportAWSConnection(),
"megaport_gcp_connection": data_megaport.MegaportGcpConnection(),
"megaport_azure_connection": data_megaport.MegaportAzureConnection(),
"megaport_oci_connection": data_megaport.MegaportOciConnection(),
"megaport_mcr": data_megaport.MegaportMCR(),
},
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
acceptTerms, ato := d.GetOk("accept_purchase_terms")
if !ato || !acceptTerms.(bool) {
return nil, errors.New(ERR_USER_NOT_ACCEPT_TOS)
}
username, password, otpKey := checkAuthVariables(d)
megaportUrl := getEnvironmentUrl(d)
deletePorts := shouldDeletePorts(d)
megaportClient := terraform_utility.MegaportClient{
DeletePorts: deletePorts,
Url: megaportUrl,
}
err := megaportClient.ConfigureServices(username, password, otpKey)
if err != nil {
return nil, err
}
return &megaportClient, nil
}
func checkAuthVariables(d *schema.ResourceData) (string, string, string) {
username := ""
password := ""
oneTimePasswordKey := ""
if u, ok := d.GetOk("username"); ok {
username = u.(string)
}
if p, ok := d.GetOk("password"); ok {
password = p.(string)
}
if otp, ok := d.GetOk("mfa_otp_key"); ok {
oneTimePasswordKey = otp.(string)
}
return username, password, oneTimePasswordKey
}
func getEnvironmentUrl(d *schema.ResourceData) string {
if environment, ok := d.GetOk("environment"); ok {
myEnvironment := environment.(string)
if myEnvironment == "production" {
return "https://api.megaport.com/"
} else if myEnvironment != "" {
return "https://api-" + myEnvironment + ".megaport.com/"
} else {
return "https://api-staging.megaport.com/"
}
} else {
return "https://api-staging.megaport.com/"
}
}
func shouldDeletePorts(d *schema.ResourceData) bool {
if shouldDelete, ok := d.GetOk("delete_ports"); ok {
return shouldDelete.(bool)
} else {
return false
}
}