-
Notifications
You must be signed in to change notification settings - Fork 9k
/
repository_data_source.go
133 lines (116 loc) · 3.8 KB
/
repository_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package ecr
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)
func DataSourceRepository() *schema.Resource {
return &schema.Resource{
Read: dataSourceRepositoryRead,
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"encryption_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"encryption_type": {
Type: schema.TypeString,
Computed: true,
},
"kms_key": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"image_scanning_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"scan_on_push": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"image_tag_mutability": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"registry_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"repository_url": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
},
}
}
func dataSourceRepositoryRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).ECRConn
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
name := d.Get("name").(string)
params := &ecr.DescribeRepositoriesInput{
RepositoryNames: aws.StringSlice([]string{name}),
}
if v, ok := d.GetOk("registry_id"); ok {
params.RegistryId = aws.String(v.(string))
}
log.Printf("[DEBUG] Reading ECR repository: %#v", params)
out, err := conn.DescribeRepositories(params)
if err != nil {
if tfawserr.ErrMessageContains(err, ecr.ErrCodeRepositoryNotFoundException, "") {
return fmt.Errorf("ECR Repository (%s) not found", name)
}
return fmt.Errorf("error reading ECR repository: %w", err)
}
repository := out.Repositories[0]
arn := aws.StringValue(repository.RepositoryArn)
d.SetId(aws.StringValue(repository.RepositoryName))
d.Set("arn", arn)
d.Set("name", repository.RepositoryName)
d.Set("registry_id", repository.RegistryId)
d.Set("repository_url", repository.RepositoryUri)
d.Set("image_tag_mutability", repository.ImageTagMutability)
if err := d.Set("image_scanning_configuration", flattenImageScanningConfiguration(repository.ImageScanningConfiguration)); err != nil {
return fmt.Errorf("error setting image_scanning_configuration for ECR Repository (%s): %w", arn, err)
}
if err := d.Set("encryption_configuration", flattenEcrRepositoryEncryptionConfiguration(repository.EncryptionConfiguration)); err != nil {
return fmt.Errorf("error setting encryption_configuration for ECR Repository (%s): %w", arn, err)
}
tags, err := ListTags(conn, arn)
// Some partitions (i.e., ISO) may not support tagging, giving error
if meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.CheckISOErrorTagsUnsupported(err) {
log.Printf("[WARN] failed listing tags for ECR Repository (%s): %s", d.Id(), err)
return nil
}
if err != nil {
return fmt.Errorf("failed listing tags for ECR Repository (%s): %w", arn, err)
}
if err := d.Set("tags", tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags for ECR Repository (%s): %w", arn, err)
}
return nil
}