Skip to content

Commit

Permalink
tsuru-cluster: make auth_provider and exec on kube_config optional bl…
Browse files Browse the repository at this point in the history
…ocks and add interactive_mode to exec
  • Loading branch information
morpheu committed Nov 30, 2023
1 parent 4c17b1b commit ada92ae
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
1 change: 1 addition & 0 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Optional:
- `args` (List of String)
- `command` (String)
- `env` (Block List) (see [below for nested schema](#nestedblock--kube_config--user--exec--env))
- `interactive_mode` (String)

<a id="nestedblock--kube_config--user--exec--env"></a>
### Nested Schema for `kube_config.user.exec.env`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/labstack/echo/v4 v4.9.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
github.com/tsuru/go-tsuruclient v0.0.0-20231108180105-5735487e6f98
github.com/tsuru/go-tsuruclient v0.0.0-20231130165047-4fa4c756fc6d
github.com/tsuru/tsuru v0.0.0-20230721211340-f41fb455f8c3
k8s.io/apimachinery v0.22.5
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ github.com/tsuru/gnuflag v0.0.0-20151217162021-86b8c1b864aa h1:JlLQP1xa13a994p/A
github.com/tsuru/gnuflag v0.0.0-20151217162021-86b8c1b864aa/go.mod h1:UibOSvkMFKRe/eiwktAPAvQG8L+p8nYsECJvu3Dgw7I=
github.com/tsuru/go-tsuruclient v0.0.0-20231108180105-5735487e6f98 h1:bjSRngH503mFLCj/1EyVxR62OF6huYMkR903VIa23BM=
github.com/tsuru/go-tsuruclient v0.0.0-20231108180105-5735487e6f98/go.mod h1:BmePxHey9hxrxk0kzTMHFFr7aJWXSxtlrUx6FIeV0Ic=
github.com/tsuru/go-tsuruclient v0.0.0-20231130165047-4fa4c756fc6d h1:oP+/hghDjbLlfka1U/IDBi9sKWdrHcb/QOYLuvxkU14=
github.com/tsuru/go-tsuruclient v0.0.0-20231130165047-4fa4c756fc6d/go.mod h1:BmePxHey9hxrxk0kzTMHFFr7aJWXSxtlrUx6FIeV0Ic=
github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6 h1:1XDdWFAjIbCSG1OjN9v9KdWhuM8UtYlFcfHe/Ldkchk=
github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6/go.mod h1:ztYpOhW+u1k21FEqp7nZNgpWbr0dUKok5lgGCZi+1AQ=
github.com/tsuru/tsuru v0.0.0-20230721211340-f41fb455f8c3 h1:Fi5mEiJiHlTCaSYpfP19kbiOzJD26yqwVojUKdLsVHA=
Expand Down
28 changes: 18 additions & 10 deletions internal/provider/resource_tsuru_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ func kubeConfigAuthExecSchema() *schema.Schema {
Type: schema.TypeString,
},
},
"interactive_mode": {
Type: schema.TypeString,
Optional: true,
Default: "Never",
},
"env": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -431,12 +436,12 @@ func kubeConfigUserFromResourceData(data interface{}) tsuru.ClusterKubeConfigUse
return user
}

func authProviderFromResourceData(data interface{}) tsuru.ClusterKubeConfigUserAuthprovider {
authProvider := tsuru.ClusterKubeConfigUserAuthprovider{}
func authProviderFromResourceData(data interface{}) *tsuru.ClusterKubeConfigUserAuthprovider {
authProvider := &tsuru.ClusterKubeConfigUserAuthprovider{}

dataArray := data.([]interface{})
if len(dataArray) == 0 {
return authProvider
return nil
}

authProviderRaw, _ := dataArray[0].(map[string]interface{})
Expand Down Expand Up @@ -467,6 +472,7 @@ func execFromResourceData(data interface{}) *tsuru.ClusterKubeConfigUserExec {

exec.ApiVersion, _ = execRaw["api_version"].(string)
exec.Command, _ = execRaw["command"].(string)
exec.InteractiveMode = execRaw["interactive_mode"].(string)

argsRaw, _ := execRaw["args"].([]interface{})
for _, arg := range argsRaw {
Expand Down Expand Up @@ -513,17 +519,18 @@ func flattenKubeConfigUser(user tsuru.ClusterKubeConfigUser) []interface{} {
"username": user.Username,
"password": user.Password,
"token": user.Token,
"auth_provider": flattenAuthProvider(user.AuthProvider),
}

if user.AuthProvider != nil {
result["auth_provider"] = flattenAuthProvider(user.AuthProvider)
}
if user.Exec != nil {
result["exec"] = flattenExec(user.Exec)
}

return []interface{}{result}
}

func flattenAuthProvider(authProvider tsuru.ClusterKubeConfigUserAuthprovider) []interface{} {
func flattenAuthProvider(authProvider *tsuru.ClusterKubeConfigUserAuthprovider) []interface{} {
result := map[string]interface{}{
"name": authProvider.Name,
"config": authProvider.Config,
Expand All @@ -534,10 +541,11 @@ func flattenAuthProvider(authProvider tsuru.ClusterKubeConfigUserAuthprovider) [

func flattenExec(exec *tsuru.ClusterKubeConfigUserExec) []interface{} {
result := map[string]interface{}{
"args": exec.Args,
"api_version": exec.ApiVersion,
"command": exec.Command,
"env": flattenEnvs(exec.Env),
"args": exec.Args,
"api_version": exec.ApiVersion,
"command": exec.Command,
"interactive_mode": exec.InteractiveMode,
"env": flattenEnvs(exec.Env),
}

return []interface{}{result}
Expand Down
11 changes: 6 additions & 5 deletions internal/provider/resource_tsuru_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestAccTsuruCluster_kubeConfig(t *testing.T) {
InsecureSkipTlsVerify: true,
},
User: tsuru.ClusterKubeConfigUser{
AuthProvider: tsuru.ClusterKubeConfigUserAuthprovider{
AuthProvider: &tsuru.ClusterKubeConfigUserAuthprovider{
Name: "tsuru",
Config: map[string]string{
"tsuru-flag-01": "result",
Expand All @@ -108,9 +108,10 @@ func TestAccTsuruCluster_kubeConfig(t *testing.T) {
ClientCertificateData: "client-cert",
ClientKeyData: "client-key",
Exec: &tsuru.ClusterKubeConfigUserExec{
ApiVersion: "api-version",
Command: "tsuru",
Args: []string{"cluster", "login"},
ApiVersion: "api-version",
Command: "tsuru",
Args: []string{"cluster", "login"},
InteractiveMode: "Never",
Env: []tsuru.ClusterKubeConfigUserExecEnv{
{
Name: "TSURU_TOKEN",
Expand All @@ -127,7 +128,7 @@ func TestAccTsuruCluster_kubeConfig(t *testing.T) {
CertificateAuthorityData: "server-cert",
},
User: tsuru.ClusterKubeConfigUser{
AuthProvider: tsuru.ClusterKubeConfigUserAuthprovider{
AuthProvider: &tsuru.ClusterKubeConfigUserAuthprovider{
Name: "mycloud",
},
},
Expand Down
8 changes: 4 additions & 4 deletions internal/provider/resource_tsuru_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func inputJobFromResourceData(ctx context.Context, d *schema.ResourceData, provi
tags = append(tags, item.(string))
}

var container tsuru_client.JobSpecContainer
var container tsuru_client.InputJobContainer

if m, ok := d.GetOk("container"); ok {
container = jobContainerFromResourceData(m)
Expand Down Expand Up @@ -331,8 +331,8 @@ func inputJobFromResourceData(ctx context.Context, d *schema.ResourceData, provi
return job, nil
}

func jobContainerFromResourceData(meta interface{}) tsuru_client.JobSpecContainer {
container := tsuru_client.JobSpecContainer{}
func jobContainerFromResourceData(meta interface{}) tsuru_client.InputJobContainer {
container := tsuru_client.InputJobContainer{}

m := meta.([]interface{})
if len(m) == 0 || m[0] == nil {
Expand All @@ -354,7 +354,7 @@ func jobContainerFromResourceData(meta interface{}) tsuru_client.JobSpecContaine
return container
}

func flattenJobContainer(container tsuru.JobSpecContainer) []interface{} {
func flattenJobContainer(container tsuru.InputJobContainer) []interface{} {

m := map[string]interface{}{
"image": container.Image,
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_tsuru_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestAccResourceTsuruJob(t *testing.T) {
Plan: tsuru.Plan{Name: "c1m1"},
Pool: "prod",
Spec: tsuru.JobSpec{
Container: tsuru.JobSpecContainer{
Container: tsuru.InputJobContainer{
Image: "tsuru/scratch:latest",
Command: []string{
"sleep",
Expand Down

0 comments on commit ada92ae

Please sign in to comment.