generated from hashicorp/terraform-provider-scaffolding
/
data_source_streaming_tenant_tokens.go
129 lines (114 loc) · 3.85 KB
/
data_source_streaming_tenant_tokens.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
package provider
import (
"context"
"fmt"
"net/http"
astrastreaming "github.com/datastax/astra-client-go/v2/astra-streaming"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceStreamingTenantTokens() *schema.Resource {
return &schema.Resource{
Description: "`astra_streaming_tenant_tokens` provides a datasource that lists streaming tenant tokens.",
ReadContext: dataSourceStreamingTenantTokensRead,
Schema: map[string]*schema.Schema{
// Required
"cluster_name": {
Description: "Name of the Pulsar Cluster. Format: `pulsar-<cloud provider>-<cloud region>`. Example: `pulsar-gcp-useast1`",
Type: schema.TypeString,
Required: true,
},
"tenant_name": {
Description: "Name of the streaming tenant for which to fetch tokens.",
Type: schema.TypeString,
Required: true,
},
// Computed
"tokens": {
Type: schema.TypeList,
Description: "The list of tokens for the specified tenant.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"iat": {
Description: "IAT of the token.",
Type: schema.TypeInt,
Computed: true,
},
"iss": {
Description: "ISS of the token.",
Type: schema.TypeString,
Computed: true,
},
"sub": {
Description: "Client subscriber of the token.",
Type: schema.TypeString,
Computed: true,
},
"token_id": {
Description: "ID of the token.",
Type: schema.TypeString,
Computed: true,
},
"token": {
Description: "Pulsar JWT token.",
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
},
},
},
}
}
func dataSourceStreamingTenantTokensRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
streamingClient := meta.(astraClients).astraStreamingClient.(*astrastreaming.ClientWithResponses)
tenantName := d.Get("tenant_name").(string)
clusterName := d.Get("cluster_name").(string)
params := astrastreaming.GetPulsarTokensByTenantParams{
XDataStaxPulsarCluster: clusterName,
}
tokenResponse, err := streamingClient.GetPulsarTokensByTenantWithResponse(ctx, tenantName, ¶ms)
if err != nil {
return diag.FromErr(err)
}
if tokenResponse.StatusCode() != http.StatusOK {
return diag.Errorf("Failed to get list of tenants. ResponseCode: %d, message = %s.", tokenResponse.StatusCode(), string(tokenResponse.Body))
}
if err := setTenantTokensData(ctx, d, streamingClient, *tokenResponse.JSON200); err != nil {
return diag.FromErr(err)
}
d.SetId(tenantName)
return nil
}
func setTenantTokensData(ctx context.Context, d *schema.ResourceData, streamingClient *astrastreaming.ClientWithResponses, tokenList []astrastreaming.TenantToken) error {
tenantName := d.Get("tenant_name").(string)
clusterName := d.Get("cluster_name").(string)
params := astrastreaming.GetPulsarTokenByIDParams{
XDataStaxPulsarCluster: clusterName,
}
tokens := make([]map[string]interface{}, 0, len(tokenList))
for _, token := range tokenList {
tokenMap := map[string]interface{}{
"token_id": token.TokenID,
"iat": token.Iat,
"iss": token.Iss,
"sub": token.Sub,
}
// now fetch the JWT token
tokenResponse, err := streamingClient.GetPulsarTokenByIDWithResponse(ctx, tenantName, *token.TokenID, ¶ms)
if err != nil {
return err
}
if tokenResponse.StatusCode() != http.StatusOK {
return fmt.Errorf("failed to fetch token by ID. Token ID: %s, Tenant Name: %s, Cluster Name: %s.", *token.TokenID, tenantName, clusterName)
}
tokenMap["token"] = string(tokenResponse.Body)
tokens = append(tokens, tokenMap)
}
if err := d.Set("tokens", tokens); err != nil {
return err
}
return nil
}