forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint_type.go
92 lines (77 loc) · 2.86 KB
/
endpoint_type.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
package parse
import (
"fmt"
"strings"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/resourceid"
)
var _ resourceid.Formatter = EndpointId{}
// TODO: remove this shim layer once the resources have been split / in 3.0
type EndpointId struct {
SubscriptionId string
ResourceGroup string
TrafficManagerProfileName string
Name string
endpointName string
}
func (id EndpointId) EndpointType() string {
return fmt.Sprintf("%sEndpoints", id.endpointName)
}
func NewEndpointId(subscriptionId, resourceGroup, trafficManagerProfile, endpoint, name string) (*EndpointId, error) {
endpoint = strings.TrimSuffix(endpoint, "Endpoints")
if endpoint != "azure" && endpoint != "external" && endpoint != "nested" {
return nil, fmt.Errorf("unsupported value for Endpoint")
}
return &EndpointId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
TrafficManagerProfileName: trafficManagerProfile,
Name: name,
endpointName: endpoint,
}, nil
}
func (id EndpointId) ID() string {
if id.endpointName == "azure" {
return NewAzureEndpointID(id.SubscriptionId, id.ResourceGroup, id.TrafficManagerProfileName, id.Name).ID()
}
if id.endpointName == "external" {
return NewExternalEndpointID(id.SubscriptionId, id.ResourceGroup, id.TrafficManagerProfileName, id.Name).ID()
}
if id.endpointName == "nested" {
return NewNestedEndpointID(id.SubscriptionId, id.ResourceGroup, id.TrafficManagerProfileName, id.Name).ID()
}
// NOTE: this looks bad but is caught above in the `New` function and below in the Parse
panic("not implemented")
}
func EndpointID(input string) (*EndpointId, error) {
azureEndpoint, err := AzureEndpointID(input)
if err == nil {
return &EndpointId{
SubscriptionId: azureEndpoint.SubscriptionId,
ResourceGroup: azureEndpoint.ResourceGroup,
TrafficManagerProfileName: azureEndpoint.TrafficManagerProfileName,
Name: azureEndpoint.Name,
endpointName: "azure",
}, nil
}
externalEndpoint, err := ExternalEndpointID(input)
if err == nil {
return &EndpointId{
SubscriptionId: externalEndpoint.SubscriptionId,
ResourceGroup: externalEndpoint.ResourceGroup,
TrafficManagerProfileName: externalEndpoint.TrafficManagerProfileName,
Name: externalEndpoint.Name,
endpointName: "external",
}, nil
}
nestedEndpoint, err := NestedEndpointID(input)
if err != nil {
return nil, err
}
return &EndpointId{
SubscriptionId: nestedEndpoint.SubscriptionId,
ResourceGroup: nestedEndpoint.ResourceGroup,
TrafficManagerProfileName: nestedEndpoint.TrafficManagerProfileName,
Name: nestedEndpoint.Name,
endpointName: "nested",
}, nil
}