Skip to content

Replace http call use api sdk #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export GO111MODULE=on
export GOPRIVATE=github.com/streamnative
export GOPRIVATE=github.com/streamnative,github.com/tuteng
20 changes: 3 additions & 17 deletions cloud/client.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
package cloud

import (
"fmt"
cloudclient "github.com/streamnative/cloud-api-server/pkg/client/clientset_generated/clientset"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
sncloudv1 "github.com/tuteng/sncloud-go-sdk"
)

func getFactoryFromMeta(meta interface{}) cmdutil.Factory {
return meta.(cmdutil.Factory)
}

func getClientSet(factory cmdutil.Factory) (*cloudclient.Clientset, error) {
config, err := factory.ToRESTConfig()
if err != nil {
return nil, fmt.Errorf("ToRESTConfig: %v", err)
}
clientSet, err := cloudclient.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("NewForConfig: %v", err)
}
return clientSet, nil
func getFactoryFromMeta(meta interface{}) *sncloudv1.APIClient {
return meta.(*sncloudv1.APIClient)
}
16 changes: 7 additions & 9 deletions cloud/data_source_pulsar_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)

Expand Down Expand Up @@ -189,14 +188,12 @@ func dataSourcePulsarCluster() *schema.Resource {
func dataSourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
namespace := d.Get("organization").(string)
name := d.Get("name").(string)
clientSet, err := getClientSet(getFactoryFromMeta(meta))
apiClient := getFactoryFromMeta(meta)
pulsarCluster, _, err := apiClient.CloudStreamnativeIoV1alpha1Api.
ReadNamespacedPulsarCluster(ctx, name, namespace).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_READ_PULSAR_CLUSTER: %w", err))
}
pulsarCluster, err := clientSet.CloudV1alpha1().PulsarClusters(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_CLUSTER: %w", err))
}
_ = d.Set("ready", "False")
if pulsarCluster.Status.Conditions != nil {
for _, condition := range pulsarCluster.Status.Conditions {
Expand Down Expand Up @@ -230,10 +227,11 @@ func dataSourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_CLUSTER_CONFIG: %w", err))
}
}
brokerImage := strings.Split(pulsarCluster.Spec.Broker.Image, ":")
brokerImage := strings.Split(*pulsarCluster.Spec.Broker.Image, ":")
_ = d.Set("pulsar_version", brokerImage[1])
bookkeeperImage := strings.Split(pulsarCluster.Spec.BookKeeper.Image, ":")
bookkeeperImage := strings.Split(*pulsarCluster.Spec.Bookkeeper.Image, ":")
_ = d.Set("bookkeeper_version", bookkeeperImage[1])
d.SetId(fmt.Sprintf("%s/%s", pulsarCluster.Namespace, pulsarCluster.Name))
metadata := pulsarCluster.GetMetadata()
d.SetId(fmt.Sprintf("%s/%s", metadata.GetNamespace(), metadata.GetName()))
return nil
}
11 changes: 4 additions & 7 deletions cloud/data_source_pulsar_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)

Expand Down Expand Up @@ -67,11 +66,8 @@ func dataSourcePulsarInstance() *schema.Resource {
func dataSourcePulsarInstanceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
namespace := d.Get("organization").(string)
name := d.Get("name").(string)
clientSet, err := getClientSet(getFactoryFromMeta(meta))
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_READ_SERVICE_ACCOUNT: %w", err))
}
pulsarInstance, err := clientSet.CloudV1alpha1().PulsarInstances(namespace).Get(ctx, name, metav1.GetOptions{})
apiClient := getFactoryFromMeta(meta)
pulsarInstance, _, err := apiClient.CloudStreamnativeIoV1alpha1Api.ReadNamespacedPulsarInstance(ctx, name, namespace).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_INSTANCE: %w", err))
}
Expand All @@ -88,6 +84,7 @@ func dataSourcePulsarInstanceRead(ctx context.Context, d *schema.ResourceData, m
_ = d.Set("pool_namespace", pulsarInstance.Spec.PoolRef.Namespace)
}
_ = d.Set("availability_mode", pulsarInstance.Spec.AvailabilityMode)
d.SetId(fmt.Sprintf("%s/%s", pulsarInstance.Namespace, pulsarInstance.Name))
metadata := pulsarInstance.GetMetadata()
d.SetId(fmt.Sprintf("%s/%s", metadata.GetNamespace(), metadata.GetName()))
return nil
}
22 changes: 8 additions & 14 deletions cloud/data_source_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)

Expand Down Expand Up @@ -57,27 +56,22 @@ func dataSourceServiceAccount() *schema.Resource {
func DataSourceServiceAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
namespace := d.Get("organization").(string)
name := d.Get("name").(string)
clientSet, err := getClientSet(getFactoryFromMeta(meta))
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_READ_SERVICE_ACCOUNT: %w", err))
}
serviceAccount, err := clientSet.CloudV1alpha1().ServiceAccounts(namespace).Get(ctx, name, metav1.GetOptions{})
apiClient := getFactoryFromMeta(meta)
serviceAccount, _, err := apiClient.CloudStreamnativeIoV1alpha1Api.
ReadNamespacedServiceAccount(ctx, name, namespace).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_READ_SERVICE_ACCOUNT: %w", err))
}
_ = d.Set("name", serviceAccount.Name)
_ = d.Set("organization", serviceAccount.Namespace)
var privateKeyData = ""
if len(serviceAccount.Status.Conditions) > 0 && serviceAccount.Status.Conditions[0].Type == "Ready" {
privateKeyData = serviceAccount.Status.PrivateKeyData
privateKeyData = *serviceAccount.Status.PrivateKeyData
}
_ = d.Set("private_key_data", privateKeyData)
if serviceAccount.Annotations != nil && serviceAccount.Annotations[ServiceAccountAdminAnnotation] == "admin" {
_ = d.Set("admin", true)
} else {
_ = d.Set("admin", false)
metadata := serviceAccount.GetMetadata()
if annotation, ok := metadata.GetAnnotations()[ServiceAccountAdminAnnotation]; ok {
_ = d.Set("admin", annotation == "admin")
}
d.SetId(fmt.Sprintf("%s/%s", serviceAccount.Namespace, serviceAccount.Name))
d.SetId(fmt.Sprintf("%s/%s", metadata.GetNamespace(), metadata.GetName()))

return nil
}
129 changes: 45 additions & 84 deletions cloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ package cloud

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mitchellh/go-homedir"
"github.com/streamnative/cloud-cli/pkg/auth"
"github.com/streamnative/cloud-cli/pkg/auth/store"
"github.com/streamnative/cloud-cli/pkg/cmd"
"github.com/streamnative/cloud-cli/pkg/config"
"github.com/streamnative/cloud-cli/pkg/plugin"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
sncloud "github.com/tuteng/sncloud-go-sdk"
"golang.org/x/oauth2/clientcredentials"
"net/url"
"os"
"path/filepath"
)

const (
GlobalDefaultIssuer = "https://auth.streamnative.cloud/"
GlobalDefaultAudience = "https://api.streamnative.cloud"
GlobalDefaultAPIServer = "https://api.streamnative.cloud"
GlobalDefaultCertificateAuthorityData = ``
ServiceAccountAdminAnnotation = "annotations.cloud.streamnative.io/service-account-role"
GlobalDefaultIssuer = "https://auth.streamnative.cloud/"
GlobalDefaultAudience = "https://api.streamnative.cloud"
GlobalDefaultAPIServer = "api.streamnative.cloud"
ServiceAccountAdminAnnotation = "annotations.cloud.streamnative.io/service-account-role"
KeyFileTypeServiceAccount = "sn_service_account"
)

type KeyFile struct {
Type string `json:"type"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
ClientEmail string `json:"client_email"`
}

var descriptions map[string]string

func init() {
Expand Down Expand Up @@ -97,19 +97,21 @@ func Provider() *schema.Provider {

func providerConfigure(d *schema.ResourceData, terraformVersion string) (interface{}, diag.Diagnostics) {
_ = terraformVersion

keyFilePath := d.Get("key_file_path").(string)
keyFile, err := os.ReadFile(keyFilePath)
if err != nil {
return nil, diag.FromErr(err)
}

home, err := homedir.Dir()
var v KeyFile
err = json.Unmarshal(keyFile, &v)
if err != nil {
return nil, diag.FromErr(err)
}
configDir := filepath.Join(home, ".streamnative")
if _, err := os.Stat(configDir); os.IsNotExist(err) {
if err = os.MkdirAll(configDir, 0755); err != nil {
return nil, diag.FromErr(err)
}
if v.Type != KeyFileTypeServiceAccount {
return nil, diag.FromErr(fmt.Errorf("open %s: unsupported format", keyFilePath))
}

defaultIssuer := os.Getenv("GLOBAL_DEFAULT_ISSUER")
if defaultIssuer == "" {
defaultIssuer = GlobalDefaultIssuer
Expand All @@ -122,71 +124,30 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
if defaultApiServer == "" {
defaultApiServer = GlobalDefaultAPIServer
}
credsProvider := auth.NewClientCredentialsProviderFromKeyFile(keyFilePath)
keyFile, err := credsProvider.GetClientCredentials()
if err != nil {
return nil, diag.FromErr(err)
}
issuer := auth.Issuer{
IssuerEndpoint: defaultIssuer,
ClientID: keyFile.ClientID,
Audience: defaultAudience,
}
flow, err := auth.NewDefaultClientCredentialsFlow(issuer, keyFilePath)
if err != nil {
return nil, diag.FromErr(err)
}
grant, err := flow.Authorize()
if err != nil {
return nil, diag.FromErr(err)
debug := os.Getenv("TF_LOG")

values := url.Values{
"audience": {defaultAudience},
}
streams := genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
config := clientcredentials.Config{
ClientID: v.ClientID,
ClientSecret: v.ClientSecret,
TokenURL: fmt.Sprintf("%soauth/token", defaultIssuer),
EndpointParams: values,
}
options := cmd.NewOptions(streams)
options.ConfigDir = configDir
options.ConfigPath = filepath.Join(configDir, "config")
options.BackendOverride = "file"
snConfig := &config.SnConfig{
Server: defaultApiServer,
CertificateAuthorityData: base64.StdEncoding.EncodeToString([]byte(GlobalDefaultCertificateAuthorityData)),
Auth: config.Auth{
IssuerEndpoint: defaultIssuer,
Audience: defaultAudience,
ClientID: keyFile.ClientID,
},
}
err = options.SaveConfig(snConfig)
token, err := config.Token(context.Background())
if err != nil {
return nil, diag.FromErr(err)
}
apc := &clientcmdapi.AuthProviderConfig{
Name: "streamnative",
}
// Pre-check if the auth provider is already exist for avoid issue
// auth Provider Plugin streamnative was registered twice
provider, _ := rest.GetAuthProvider("", apc, nil)
if provider == nil {
err = options.Complete()
if err != nil {
return nil, diag.FromErr(err)
}
configuration := sncloud.NewConfiguration()
configuration.Host = defaultApiServer
configuration.Scheme = "https"
if debug == "debug" {
configuration.Debug = true
} else {
options.Store = store.NewMemoryStore()
options.Factory, err = plugin.NewDefaultFactory(options.Store, func() (auth.Issuer, error) {
return issuer, nil
})
err = options.ServerOptions.Complete(options)
if err != nil {
return nil, diag.FromErr(err)
}
}
err = options.Store.SaveGrant(issuer.Audience, *grant)
if err != nil {
return nil, diag.FromErr(err)
configuration.Debug = false
}
factory := cmdutil.NewFactory(options)
return factory, nil
configuration.AddDefaultHeader("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken))
apiClient := sncloud.NewAPIClient(configuration)
return apiClient, nil
}
10 changes: 5 additions & 5 deletions cloud/pulsar_cluster_config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cloud

import (
cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
sncloudv1 "github.com/tuteng/sncloud-go-sdk"
)

func flattenPulsarClusterConfig(in *cloudv1alpha1.Config) []interface{} {
func flattenPulsarClusterConfig(in *sncloudv1.V1alpha1Config) []interface{} {
att := make(map[string]interface{})
if in.WebsocketEnabled != nil {
att["websocket_enabled"] = in.WebsocketEnabled
Expand All @@ -23,13 +23,13 @@ func flattenPulsarClusterConfig(in *cloudv1alpha1.Config) []interface{} {
att["audit_log"] = flattenAuditLog(in.AuditLog)
}
if in.Custom != nil {
att["custom"] = in.Custom
att["custom"] = *in.Custom
}

return []interface{}{att}
}

func flattenProtocols(in *cloudv1alpha1.ProtocolsConfig) []interface{} {
func flattenProtocols(in *sncloudv1.V1alpha1ProtocolsConfig) []interface{} {
att := make(map[string]interface{})
if in.Kafka != nil {
att["kafka"] = flattenKafkaConfig("true")
Expand All @@ -52,7 +52,7 @@ func flattenMqttConfig(flag string) map[string]interface{} {
return map[string]interface{}{"enabled": flag}
}

func flattenAuditLog(in *cloudv1alpha1.AuditLog) []interface{} {
func flattenAuditLog(in *sncloudv1.V1alpha1AuditLog) []interface{} {
att := make(map[string]interface{})
if in.Categories != nil {
att["categories"] = flattenCategories(in.Categories)
Expand Down
21 changes: 6 additions & 15 deletions cloud/pulsar_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -41,14 +40,10 @@ func testCheckPulsarClusterDestroy(s *terraform.State) error {
continue
}
meta := testAccProvider.Meta()
clientSet, err := getClientSet(getFactoryFromMeta(meta))
if err != nil {
return err
}
apiClient := getFactoryFromMeta(meta)
organizationCluster := strings.Split(rs.Primary.ID, "/")
_, err = clientSet.CloudV1alpha1().
PulsarClusters(organizationCluster[0]).
Get(context.Background(), organizationCluster[1], metav1.GetOptions{})
_, _, err := apiClient.CloudStreamnativeIoV1alpha1Api.ReadNamespacedPulsarCluster(
context.Background(), organizationCluster[1], organizationCluster[0]).Execute()
if err != nil {
if errors.IsNotFound(err) {
return nil
Expand All @@ -70,14 +65,10 @@ func testCheckPulsarClusterExists(name string) resource.TestCheckFunc {
return fmt.Errorf("ERROR_RESOURCE_PULSAR_INSTANCE_ID_NOT_SET")
}
meta := testAccProvider.Meta()
clientSet, err := getClientSet(getFactoryFromMeta(meta))
if err != nil {
return err
}
apiClient := getFactoryFromMeta(meta)
organizationCluster := strings.Split(rs.Primary.ID, "/")
pulsarCluster, err := clientSet.CloudV1alpha1().
PulsarClusters(organizationCluster[0]).
Get(context.Background(), organizationCluster[1], metav1.GetOptions{})
pulsarCluster, _, err := apiClient.CloudStreamnativeIoV1alpha1Api.ReadNamespacedPulsarCluster(
context.Background(), organizationCluster[1], organizationCluster[0]).Execute()
if err != nil {
return err
}
Expand Down
Loading