forked from hashicorp/terraform-provider-aws
/
resource_aws_msk_configuration.go
137 lines (110 loc) · 3.58 KB
/
resource_aws_msk_configuration.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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kafka"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsMskConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceAwsMskConfigurationCreate,
Read: resourceAwsMskConfigurationRead,
Delete: schema.Noop,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"kafka_versions": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"latest_revision": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"server_properties": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsMskConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kafkaconn
input := &kafka.CreateConfigurationInput{
KafkaVersions: expandStringSet(d.Get("kafka_versions").(*schema.Set)),
Name: aws.String(d.Get("name").(string)),
ServerProperties: []byte(d.Get("server_properties").(string)),
}
if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}
output, err := conn.CreateConfiguration(input)
if err != nil {
return fmt.Errorf("error creating MSK Configuration: %s", err)
}
d.SetId(aws.StringValue(output.Arn))
return resourceAwsMskConfigurationRead(d, meta)
}
func resourceAwsMskConfigurationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kafkaconn
configurationInput := &kafka.DescribeConfigurationInput{
Arn: aws.String(d.Id()),
}
configurationOutput, err := conn.DescribeConfiguration(configurationInput)
if isAWSErr(err, kafka.ErrCodeNotFoundException, "") {
log.Printf("[WARN] MSK Configuration (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("error describing MSK Configuration (%s): %s", d.Id(), err)
}
if configurationOutput == nil {
return fmt.Errorf("error describing MSK Configuration (%s): missing result", d.Id())
}
if configurationOutput.LatestRevision == nil {
return fmt.Errorf("error describing MSK Configuration (%s): missing latest revision", d.Id())
}
revision := configurationOutput.LatestRevision.Revision
revisionInput := &kafka.DescribeConfigurationRevisionInput{
Arn: aws.String(d.Id()),
Revision: revision,
}
revisionOutput, err := conn.DescribeConfigurationRevision(revisionInput)
if err != nil {
return fmt.Errorf("error describing MSK Configuration (%s) Revision (%d): %s", d.Id(), aws.Int64Value(revision), err)
}
if revisionOutput == nil {
return fmt.Errorf("error describing MSK Configuration (%s) Revision (%d): missing result", d.Id(), aws.Int64Value(revision))
}
d.Set("arn", aws.StringValue(configurationOutput.Arn))
d.Set("description", aws.StringValue(configurationOutput.Description))
if err := d.Set("kafka_versions", aws.StringValueSlice(configurationOutput.KafkaVersions)); err != nil {
return fmt.Errorf("error setting kafka_versions: %s", err)
}
d.Set("latest_revision", aws.Int64Value(revision))
d.Set("name", aws.StringValue(configurationOutput.Name))
d.Set("server_properties", string(revisionOutput.ServerProperties))
return nil
}