-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
connection_data_source.go
115 lines (101 loc) · 3.37 KB
/
connection_data_source.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package directconnect
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/directconnect"
awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)
// @SDKDataSource("aws_dx_connection", name="Connection")
func dataSourceConnection() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceConnectionRead,
Schema: map[string]*schema.Schema{
names.AttrARN: {
Type: schema.TypeString,
Computed: true,
},
"aws_device": {
Type: schema.TypeString,
Computed: true,
},
"bandwidth": {
Type: schema.TypeString,
Computed: true,
},
names.AttrLocation: {
Type: schema.TypeString,
Computed: true,
},
names.AttrName: {
Type: schema.TypeString,
Required: true,
},
names.AttrOwnerAccountID: {
Type: schema.TypeString,
Computed: true,
},
"partner_name": {
Type: schema.TypeString,
Computed: true,
},
names.AttrProviderName: {
Type: schema.TypeString,
Computed: true,
},
names.AttrTags: tftags.TagsSchemaComputed(),
"vlan_id": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).DirectConnectClient(ctx)
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig(ctx)
name := d.Get(names.AttrName).(string)
input := &directconnect.DescribeConnectionsInput{}
connection, err := findConnection(ctx, conn, input, func(v *awstypes.Connection) bool {
return aws.ToString(v.ConnectionName) == name
})
if err != nil {
return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("Direct Connect Connection", err))
}
d.SetId(aws.ToString(connection.ConnectionId))
arn := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition(ctx),
Region: aws.ToString(connection.Region),
Service: "directconnect",
AccountID: aws.ToString(connection.OwnerAccount),
Resource: fmt.Sprintf("dxcon/%s", d.Id()),
}.String()
d.Set(names.AttrARN, arn)
d.Set("aws_device", connection.AwsDeviceV2)
d.Set("bandwidth", connection.Bandwidth)
d.Set(names.AttrLocation, connection.Location)
d.Set(names.AttrName, connection.ConnectionName)
d.Set(names.AttrOwnerAccountID, connection.OwnerAccount)
d.Set("partner_name", connection.PartnerName)
d.Set(names.AttrProviderName, connection.ProviderName)
d.Set("vlan_id", connection.Vlan)
tags, err := listTags(ctx, conn, arn)
if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for Direct Connect Connection (%s): %s", arn, err)
}
if err := d.Set(names.AttrTags, tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "setting tags: %s", err)
}
return diags
}