13
13
// See the License for the specific language governing permissions and
14
14
// limitations under the License.
15
15
16
- // Package clients provides the CloudV2 clients used by the Redpanda terraform
17
- // provider and the generated resources .
18
- package clients
16
+ // Package cloud provides the methods to connect and talk to the Redpanda Cloud
17
+ // public API .
18
+ package cloud
19
19
20
20
import (
21
21
"context"
@@ -36,15 +36,15 @@ import (
36
36
"google.golang.org/grpc/metadata"
37
37
)
38
38
39
- // cloudEndpoint is a representation of a cloud V2 endpoint, containing the URLs
40
- // for authentication and the API URL.
41
- type cloudEndpoint struct {
42
- apiURL string // CloudV2 public API URL.
39
+ // Endpoint is a representation of a cloud endpoint for a single environment. It
40
+ // contains the URLs, audience for authentication and the API URL.
41
+ type Endpoint struct {
42
+ APIURL string // CloudV2 public API URL.
43
43
authURL string // CloudV2 URL for authorization token exchange.
44
44
audience string // CloudV2 audience used for token exchange.
45
45
}
46
46
47
- var cloudAuthEnvironments = map [string ]cloudEndpoint {
47
+ var endpoints = map [string ]Endpoint {
48
48
"dev" : {
49
49
"api.dev.cloud.redpanda.com:443" ,
50
50
"https://dev-cloudv2.us.auth0.com/oauth/token" ,
@@ -62,33 +62,27 @@ var cloudAuthEnvironments = map[string]cloudEndpoint{
62
62
},
63
63
}
64
64
65
- // ClientRequest are the client request credentials used to create a connection.
66
- type ClientRequest struct {
67
- ClientID string
68
- ClientSecret string
69
- // TODO: we can use this as the only source of truth for Client Credentials and Envs.
70
- }
71
-
72
65
type tokenResponse struct {
73
66
AccessToken string `json:"access_token"`
74
67
Scope string `json:"scope"`
75
68
ExpiresIn int `json:"expires_in"`
76
69
TokenType string `json:"token_type"`
77
70
}
78
71
79
- // requestTokenAndEnv requests a token.
80
- func requestTokenAndEnv (ctx context.Context , cloudEnv string , cr ClientRequest ) (string , * cloudEndpoint , error ) {
81
- if cr .ClientID == "" {
72
+ // RequestTokenAndEnv requests an authentication token and return the Endpoint
73
+ // for a given environment.
74
+ func RequestTokenAndEnv (ctx context.Context , cloudEnv , clientID , clientSecret string ) (string , * Endpoint , error ) {
75
+ if clientID == "" {
82
76
return "" , nil , fmt .Errorf ("client_id is not set" )
83
77
}
84
- if cr . ClientSecret == "" {
78
+ if clientSecret == "" {
85
79
return "" , nil , fmt .Errorf ("client_secret is not set" )
86
80
}
87
- endpoint , found := cloudAuthEnvironments [cloudEnv ]
81
+ endpoint , found := endpoints [cloudEnv ]
88
82
if ! found {
89
83
return "" , nil , fmt .Errorf ("unable to find requested environment: %q" , cloudEnv )
90
84
}
91
- payload := fmt .Sprintf ("grant_type=client_credentials&client_id=%s&client_secret=%s&audience=%s" , cr . ClientID , cr . ClientSecret , endpoint .audience )
85
+ payload := fmt .Sprintf ("grant_type=client_credentials&client_id=%s&client_secret=%s&audience=%s" , clientID , clientSecret , endpoint .audience )
92
86
req , err := http .NewRequestWithContext (ctx , "POST" , endpoint .authURL , strings .NewReader (payload ))
93
87
if err != nil {
94
88
return "" , nil , fmt .Errorf ("unable to issue request to %v: %v" , endpoint .authURL , err )
@@ -118,7 +112,9 @@ func requestTokenAndEnv(ctx context.Context, cloudEnv string, cr ClientRequest)
118
112
return tokenContainer .AccessToken , & endpoint , nil
119
113
}
120
114
121
- func spawnConn (ctx context.Context , url string , authToken string ) (* grpc.ClientConn , error ) {
115
+ // SpawnConn returns a grpc connection to the given URL, it adds a bearer token
116
+ // to each request with the given 'authToken'.
117
+ func SpawnConn (ctx context.Context , url string , authToken string ) (* grpc.ClientConn , error ) {
122
118
return grpc .DialContext (
123
119
ctx ,
124
120
url ,
0 commit comments