/
data_source_private_link_attachment.go
155 lines (143 loc) · 5.11 KB
/
data_source_private_link_attachment.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
154
155
// Copyright 2023 Confluent Inc. All Rights Reserved.
//
// Licensed under the Apache 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
//
// http://www.apache.org/licenses/LICENSE-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 provider
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
paramVpcEndpointServiceName = "vpc_endpoint_service_name"
stateWaitingForConnections = "WAITING_FOR_CONNECTIONS"
paramZone = "zone"
paramPrivateLinkServiceAlias = "private_link_service_alias"
paramPrivateLinkServiceResourceId = "private_link_service_resource_id"
paramPrivateServiceConnectServiceAttachment = "private_service_connect_service_attachment"
)
func privateLinkAttachmentDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: privateLinkAttachmentDataSourceRead,
Schema: map[string]*schema.Schema{
paramId: {
Type: schema.TypeString,
Required: true,
Description: "The ID of the Private Link Attachment, for example, `platt-61ovvd`.",
},
paramEnvironment: environmentDataSourceSchema(),
paramDisplayName: {
Type: schema.TypeString,
Computed: true,
Description: "The display name of the Private Link Attachment.",
},
paramCloud: {
Type: schema.TypeString,
Computed: true,
},
paramRegion: {
Type: schema.TypeString,
Computed: true,
},
paramResourceName: {
Type: schema.TypeString,
Computed: true,
},
paramDnsDomain: {
Type: schema.TypeString,
Computed: true,
Description: "The root DNS domain for the private link attachment.",
},
paramAws: awsVpcEndpointServiceSchema(),
paramAzure: azurePrivateLinkServicesSchema(),
paramGcp: gcpServiceAttachmentsSchema(),
},
}
}
func awsVpcEndpointServiceSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramVpcEndpointServiceName: {
Type: schema.TypeString,
Computed: true,
Description: "Id of the VPC Endpoint service.",
},
},
},
}
}
func azurePrivateLinkServicesSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramPrivateLinkServiceAlias: {
Type: schema.TypeString,
Computed: true,
Description: "Azure PrivateLink service alias for the availability zone.",
},
paramPrivateLinkServiceResourceId: {
Type: schema.TypeString,
Computed: true,
Description: "Azure PrivateLink service resource id for the availability zone.",
},
},
},
}
}
func gcpServiceAttachmentsSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramZone: {
Type: schema.TypeString,
Computed: true,
Description: "Zone associated with the PSC Service attachment.",
},
paramPrivateServiceConnectServiceAttachment: {
Type: schema.TypeString,
Computed: true,
Description: "Id of a Private Service Connect Service Attachment in Confluent Cloud.",
},
},
},
}
}
func privateLinkAttachmentDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
plattId := d.Get(paramId).(string)
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
tflog.Debug(ctx, fmt.Sprintf("Reading Private Link Attachment %q=%q", paramId, plattId), map[string]interface{}{privateLinkAttachmentLoggingKey: plattId})
c := meta.(*Client)
request := c.netPLClient.PrivateLinkAttachmentsNetworkingV1Api.GetNetworkingV1PrivateLinkAttachment(c.netPLApiContext(ctx), plattId).Environment(environmentId)
platt, _, err := c.netPLClient.PrivateLinkAttachmentsNetworkingV1Api.GetNetworkingV1PrivateLinkAttachmentExecute(request)
if err != nil {
return diag.Errorf("error reading Private Link Attachment %q: %s", plattId, createDescriptiveError(err))
}
plattJson, err := json.Marshal(platt)
if err != nil {
return diag.Errorf("error reading Private Link Attachment %q: error marshaling %#v to json: %s", plattId, platt, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Fetched Private Link Attachment %q: %s", plattId, plattJson), map[string]interface{}{privateLinkAttachmentLoggingKey: plattId})
if _, err := setPrivateLinkAttachmentAttributes(d, platt); err != nil {
return diag.FromErr(createDescriptiveError(err))
}
return nil
}