Skip to content

Commit

Permalink
feat: return file content on AsEnv()
Browse files Browse the repository at this point in the history
* added tests as well
  • Loading branch information
adityathebe committed Oct 18, 2023
1 parent b7bfe12 commit 84f3037
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
20 changes: 17 additions & 3 deletions models/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"regexp"
"strings"
"time"

"github.com/flanksource/duty/types"
Expand Down Expand Up @@ -151,21 +152,34 @@ func (c Connection) AsGoGetterURL() (string, error) {
return output, nil
}

func (c Connection) AsEnv() []string {
var envs []string
// AsEnv generates environment variables and a configuration file content based on the connection type.
func (c Connection) AsEnv() ([]string, string) {
var (
envs []string
file strings.Builder
)

switch c.Type {
case ConnectionTypeAWS:
envs = append(envs, fmt.Sprintf("AWS_ACCESS_KEY_ID=%s", c.Username))
envs = append(envs, fmt.Sprintf("AWS_SECRET_ACCESS_KEY=%s", c.Password))

file.WriteString("[default]\n")
file.WriteString(fmt.Sprintf("aws_access_key_id = %s\n", c.Username))
file.WriteString(fmt.Sprintf("aws_secret_access_key = %s\n", c.Password))

if v, ok := c.Properties["profile"]; ok {
envs = append(envs, fmt.Sprintf("AWS_DEFAULT_PROFILE=%s", v))
}

if v, ok := c.Properties["region"]; ok {
envs = append(envs, fmt.Sprintf("AWS_DEFAULT_REGION=%s", v))
file.WriteString(fmt.Sprintf("region = %s\n", v))
}

case ConnectionTypeGCP:
file.WriteString(c.Certificate)
}

return envs
return envs, file.String()
}
103 changes: 103 additions & 0 deletions models/connections_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package models

import (
"testing"
)

func Test_Connection_AsGoGetterURL(t *testing.T) {
testCases := []struct {
name string
connection Connection
expectedURL string
expectedError error
}{
{
name: "HTTP Connection",
connection: Connection{
Type: ConnectionTypeHTTP,
URL: "http://example.com",
Username: "testuser",
Password: "testpassword",
},
expectedURL: "http://testuser:testpassword@example.com",
expectedError: nil,
},
{
name: "Git Connection",
connection: Connection{
Type: ConnectionTypeGit,
URL: "https://github.com/repo.git",
Certificate: "cert123",
Properties: map[string]string{"ref": "main"},
},
expectedURL: "https://github.com/repo.git?ref=main&sshkey=cert123",
expectedError: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resultURL, err := tc.connection.AsGoGetterURL()

if resultURL != tc.expectedURL {
t.Errorf("Expected URL: %s, but got: %s", tc.expectedURL, resultURL)
}

if err != tc.expectedError {
t.Errorf("Expected error: %v, but got: %v", tc.expectedError, err)
}
})
}
}

func Test_Connection_AsEnv(t *testing.T) {
testCases := []struct {
name string
connection Connection
expectedEnv []string
expectedFile string
}{
{
name: "AWS Connection",
connection: Connection{
Type: ConnectionTypeAWS,
Username: "awsuser",
Password: "awssecret",
Properties: map[string]string{"profile": "awsprofile", "region": "us-east-1"},
},
expectedEnv: []string{
"AWS_ACCESS_KEY_ID=awsuser",
"AWS_SECRET_ACCESS_KEY=awssecret",
"AWS_DEFAULT_PROFILE=awsprofile",
"AWS_DEFAULT_REGION=us-east-1",
},
expectedFile: "[default]\naws_access_key_id = awsuser\naws_secret_access_key = awssecret\nregion = us-east-1\n",
},
{
name: "GCP Connection",
connection: Connection{
Type: ConnectionTypeGCP,
Username: "gcpuser",
Certificate: `{"account": "gcpuser"}`,
},
expectedEnv: []string{},
expectedFile: `{"account": "gcpuser"}`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
env, file := tc.connection.AsEnv()

for i, expected := range tc.expectedEnv {
if env[i] != expected {
t.Errorf("Expected environment variable: %s, but got: %s", expected, env[i])
}
}

if file != tc.expectedFile {
t.Errorf("Expected file content:\n%s\nBut got:\n%s", tc.expectedFile, file)
}
})
}
}

0 comments on commit 84f3037

Please sign in to comment.