@@ -84,9 +84,8 @@ func resourceStreamingTopicDelete(ctx context.Context, resourceData *schema.Reso
84
84
if protectedFromDelete (resourceData ) {
85
85
return diag .Errorf ("\" deletion_protection\" must be explicitly set to \" false\" in order to destroy astra_streaming_topic" )
86
86
}
87
- streamingClient := meta .(astraClients ).astraStreamingClient .(* astrastreaming.ClientWithResponses )
88
- client := meta .(astraClients ).astraClient .(* astra.ClientWithResponses )
89
- streamingClientv3 := meta .(astraClients ).astraStreamingClientv3
87
+ astraClients := meta .(astraClients )
88
+ astraClient := astraClients .astraClient .(* astra.ClientWithResponses )
90
89
91
90
namespace := resourceData .Get ("namespace" ).(string )
92
91
topic := resourceData .Get ("topic" ).(string )
@@ -95,47 +94,46 @@ func resourceStreamingTopicDelete(ctx context.Context, resourceData *schema.Reso
95
94
cloudProvider := resourceData .Get ("cloud_provider" ).(string )
96
95
rawRegion := resourceData .Get ("region" ).(string )
97
96
98
- token := meta .( astraClients ) .token
97
+ token := astraClients .token
99
98
100
99
region := strings .ReplaceAll (rawRegion , "-" , "" )
101
- pulsarCluster := getPulsarCluster (cloudProvider , region )
102
- orgBody , _ := client .GetCurrentOrganization (ctx )
103
-
100
+ pulsarCluster := getPulsarCluster (cloudProvider , region , astraClients .streamingTestMode )
101
+ orgResp , err := astraClient .GetCurrentOrganization (ctx )
102
+ if err != nil {
103
+ return diag .Errorf ("Failed to read orgnization ID: %v" , err )
104
+ }
104
105
var org OrgId
105
- bodyBuffer , err := ioutil .ReadAll (orgBody .Body )
106
-
107
- err = json .Unmarshal (bodyBuffer , & org )
106
+ err = json .NewDecoder (orgResp .Body ).Decode (& org )
108
107
if err != nil {
109
- fmt . Println ( "Can't deserislize " , orgBody )
108
+ return diag . Errorf ( "Failed to read orgnization ID: %v " , err )
110
109
}
111
110
112
- pulsarToken , err := getPulsarToken (ctx , pulsarCluster , token , org , err , streamingClient , tenant )
111
+ pulsarToken , err := getLatestPulsarToken (ctx , astraClients .astraStreamingClientv3 , token , org .ID , pulsarCluster , tenant )
112
+ if err != nil {
113
+ return diag .FromErr (err )
114
+ }
113
115
114
116
deleteTopicParams := astrastreaming.DeleteTopicParams {
115
117
XDataStaxPulsarCluster : pulsarCluster ,
116
118
Authorization : fmt .Sprintf ("Bearer %s" , pulsarToken ),
117
119
}
118
120
119
- deleteTopicResponse , err := streamingClientv3 .DeleteTopic (ctx , tenant , namespace , topic , & deleteTopicParams )
121
+ deleteTopicResponse , err := astraClients . astraStreamingClientv3 .DeleteTopic (ctx , tenant , namespace , topic , & deleteTopicParams )
120
122
if err != nil {
121
- diag .FromErr (err )
122
- }
123
-
124
- if ! strings .HasPrefix (deleteTopicResponse .Status , "2" ) {
125
- bodyBuffer , err = ioutil .ReadAll (deleteTopicResponse .Body )
123
+ return diag .FromErr (err )
124
+ } else if deleteTopicResponse .StatusCode > 299 {
125
+ bodyBuffer , _ := ioutil .ReadAll (deleteTopicResponse .Body )
126
126
return diag .Errorf ("Error deleting topic %s" , bodyBuffer )
127
127
}
128
- bodyBuffer , err = ioutil .ReadAll (deleteTopicResponse .Body )
129
128
130
129
resourceData .SetId ("" )
131
130
132
131
return nil
133
132
}
134
133
135
134
func resourceStreamingTopicRead (ctx context.Context , resourceData * schema.ResourceData , meta interface {}) diag.Diagnostics {
136
- streamingClient := meta .(astraClients ).astraStreamingClient .(* astrastreaming.ClientWithResponses )
137
- client := meta .(astraClients ).astraClient .(* astra.ClientWithResponses )
138
- streamingClientv3 := meta .(astraClients ).astraStreamingClientv3
135
+ astraClients := meta .(astraClients )
136
+ astraClient := astraClients .astraClient .(* astra.ClientWithResponses )
139
137
140
138
namespace := resourceData .Get ("namespace" ).(string )
141
139
topic := resourceData .Get ("topic" ).(string )
@@ -144,41 +142,37 @@ func resourceStreamingTopicRead(ctx context.Context, resourceData *schema.Resour
144
142
cloudProvider := resourceData .Get ("cloud_provider" ).(string )
145
143
rawRegion := resourceData .Get ("region" ).(string )
146
144
147
- token := meta .( astraClients ) .token
145
+ token := astraClients .token
148
146
149
147
region := strings .ReplaceAll (rawRegion , "-" , "" )
150
- pulsarCluster := getPulsarCluster (cloudProvider , region )
151
- orgBody , _ := client .GetCurrentOrganization (ctx )
152
-
148
+ pulsarCluster := getPulsarCluster (cloudProvider , region , astraClients .streamingTestMode )
149
+ orgBody , err := astraClient .GetCurrentOrganization (ctx )
150
+ if err != nil {
151
+ return diag .Errorf ("Failed to get organization ID: %v" , err )
152
+ }
153
153
var org OrgId
154
- bodyBuffer , err := ioutil . ReadAll (orgBody .Body )
154
+ err = json . NewDecoder (orgBody .Body ). Decode ( & org )
155
155
if err != nil {
156
- return diag .FromErr ( err )
156
+ return diag .Errorf ( "Failed to read organization ID: %v" , err )
157
157
}
158
158
159
- err = json . Unmarshal ( bodyBuffer , & org )
159
+ pulsarToken , err := getLatestPulsarToken ( ctx , astraClients . astraStreamingClientv3 , token , org . ID , pulsarCluster , tenant )
160
160
if err != nil {
161
- fmt . Println ( "Can't deserislize " , orgBody )
161
+ return diag . Errorf ( "Failed to get pulsar token: %v " , err )
162
162
}
163
163
164
- pulsarToken , err := getPulsarToken (ctx , pulsarCluster , token , org , err , streamingClient , tenant )
165
-
166
164
getTopicsParams := astrastreaming.GetTopicsParams {
167
165
XDataStaxPulsarCluster : & pulsarCluster ,
168
166
Authorization : fmt .Sprintf ("Bearer %s" , pulsarToken ),
169
167
}
170
168
171
- createTopicResponse , err := streamingClientv3 .GetTopics (ctx , tenant , namespace , & getTopicsParams )
169
+ readTopicResponse , err := astraClients . astraStreamingClientv3 .GetTopics (ctx , tenant , namespace , & getTopicsParams )
172
170
if err != nil {
173
- return diag .FromErr (err )
174
- }
175
-
176
- if ! strings .HasPrefix (createTopicResponse .Status , "2" ) {
177
- bodyBuffer , err = ioutil .ReadAll (createTopicResponse .Body )
171
+ return diag .Errorf ("Failed to get topic list: %v" , err )
172
+ } else if readTopicResponse .StatusCode > 299 {
173
+ bodyBuffer , _ := ioutil .ReadAll (readTopicResponse .Body )
178
174
return diag .Errorf ("Error reading topic %s" , bodyBuffer )
179
175
}
180
- bodyBuffer , err = ioutil .ReadAll (createTopicResponse .Body )
181
-
182
176
//TODO: validate that our topic is there
183
177
184
178
setStreamingTopicData (resourceData , tenant , topic )
@@ -187,9 +181,8 @@ func resourceStreamingTopicRead(ctx context.Context, resourceData *schema.Resour
187
181
}
188
182
189
183
func resourceStreamingTopicCreate (ctx context.Context , resourceData * schema.ResourceData , meta interface {}) diag.Diagnostics {
190
- streamingClient := meta .(astraClients ).astraStreamingClient .(* astrastreaming.ClientWithResponses )
191
- client := meta .(astraClients ).astraClient .(* astra.ClientWithResponses )
192
- streamingClientv3 := meta .(astraClients ).astraStreamingClientv3
184
+ astraClients := meta .(astraClients )
185
+ astraClient := astraClients .astraClient .(* astra.ClientWithResponses )
193
186
194
187
namespace := resourceData .Get ("namespace" ).(string )
195
188
topic := resourceData .Get ("topic" ).(string )
@@ -198,38 +191,39 @@ func resourceStreamingTopicCreate(ctx context.Context, resourceData *schema.Reso
198
191
cloudProvider := resourceData .Get ("cloud_provider" ).(string )
199
192
rawRegion := resourceData .Get ("region" ).(string )
200
193
201
- token := meta .( astraClients ) .token
194
+ token := astraClients .token
202
195
203
196
region := strings .ReplaceAll (rawRegion , "-" , "" )
204
- pulsarCluster := getPulsarCluster (cloudProvider , region )
205
- orgBody , _ := client .GetCurrentOrganization (ctx )
206
-
207
- var org OrgId
208
- bodyBuffer , err := ioutil .ReadAll (orgBody .Body )
197
+ pulsarCluster := getPulsarCluster (cloudProvider , region , astraClients .streamingTestMode )
209
198
210
- err = json . Unmarshal ( bodyBuffer , & org )
199
+ orgResp , err := astraClient . GetCurrentOrganization ( ctx )
211
200
if err != nil {
212
- fmt .Println ("Can't deserislize" , orgBody )
201
+ return diag .Errorf ("Failed to get current organization: %v" , err )
202
+ }
203
+ org := OrgId {}
204
+ err = json .NewDecoder (orgResp .Body ).Decode (& org )
205
+ if err != nil {
206
+ return diag .Errorf ("Failed to read organization: %v" , err )
213
207
}
214
208
215
- pulsarToken , err := getPulsarToken (ctx , pulsarCluster , token , org , err , streamingClient , tenant )
209
+ pulsarToken , err := getLatestPulsarToken (ctx , astraClients .astraStreamingClientv3 , token , org .ID , pulsarCluster , tenant )
210
+ if err != nil {
211
+ return diag .FromErr (err )
212
+ }
216
213
217
214
createTopicParams := astrastreaming.CreateTopicParams {
218
215
XDataStaxCurrentOrg : & org .ID ,
219
216
XDataStaxPulsarCluster : pulsarCluster ,
220
217
Authorization : fmt .Sprintf ("Bearer %s" , pulsarToken ),
221
218
}
222
219
223
- createTopicResponse , err := streamingClientv3 . CreateTopic (ctx , tenant , namespace , topic , & createTopicParams )
220
+ createTopicResponse , err := astraClients . astraStreamingClientv3 . CreateTopic (ctx , tenant , namespace , topic , & createTopicParams , setContentTypeHeader ( "application/json" ) )
224
221
if err != nil {
225
- diag .FromErr (err )
226
- }
227
-
228
- if ! strings .HasPrefix (createTopicResponse .Status , "2" ) {
229
- bodyBuffer , err = ioutil .ReadAll (createTopicResponse .Body )
222
+ return diag .Errorf ("Error creating topic: %v" , err )
223
+ } else if createTopicResponse .StatusCode > 299 {
224
+ bodyBuffer , _ := ioutil .ReadAll (createTopicResponse .Body )
230
225
return diag .Errorf ("Error creating topic %s" , bodyBuffer )
231
226
}
232
- bodyBuffer , err = ioutil .ReadAll (createTopicResponse .Body )
233
227
234
228
setStreamingTopicData (resourceData , tenant , topic )
235
229
0 commit comments