From b7bfe1270a1baf8269f492bc5a75ccb09ea359df Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 18 Oct 2023 11:10:11 +0545 Subject: [PATCH] feat: add AsGoGetterURL() to connection --- models/connections.go | 121 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/models/connections.go b/models/connections.go index 28c09d07..024f4289 100644 --- a/models/connections.go +++ b/models/connections.go @@ -1,6 +1,7 @@ package models import ( + "fmt" "net/url" "regexp" "time" @@ -9,6 +10,50 @@ import ( "github.com/google/uuid" ) +// List of all connection types +const ( + ConnectionTypeAWS = "AWS" + ConnectionTypeAzure = "Azure" + ConnectionTypeAzureDevops = "Azure Devops" + ConnectionTypeDiscord = "Discord" + ConnectionTypeDynatrace = "Dynatrace" + ConnectionTypeElasticSearch = "ElasticSearch" + ConnectionTypeEmail = "Email" + ConnectionTypeGCP = "Google Cloud" + ConnectionTypeGenericWebhook = "Generic Webhook" + ConnectionTypeGit = "Git" + ConnectionTypeGithub = "Github" + ConnectionTypeGoogleChat = "Google Chat" + ConnectionTypeHTTP = "HTTP" + ConnectionTypeIFTTT = "IFTTT" + ConnectionTypeJMeter = "JMeter" + ConnectionTypeKubernetes = "Kubernetes" + ConnectionTypeLDAP = "LDAP" + ConnectionTypeMatrix = "Matrix" + ConnectionTypeMattermost = "Mattermost" + ConnectionTypeMongo = "Mongo" + ConnectionTypeMySQL = "MySQL" + ConnectionTypeNtfy = "Ntfy" + ConnectionTypeOpsGenie = "OpsGenie" + ConnectionTypePostgres = "Postgres" + ConnectionTypePrometheus = "Prometheus" + ConnectionTypePushbullet = "Pushbullet" + ConnectionTypePushover = "Pushover" + ConnectionTypeRedis = "Redis" + ConnectionTypeRestic = "Restic" + ConnectionTypeRocketchat = "Rocketchat" + ConnectionTypeSFTP = "SFTP" + ConnectionTypeSlack = "Slack" + ConnectionTypeSlackWebhook = "SlackWebhook" + ConnectionTypeSMB = "SMB" + ConnectionTypeSQLServer = "SQL Server" + ConnectionTypeTeams = "Teams" + ConnectionTypeTelegram = "Telegram" + ConnectionTypeWebhook = "Webhook" + ConnectionTypeWindows = "Windows" + ConnectionTypeZulipChat = "Zulip Chat" +) + type Connection struct { ID uuid.UUID `gorm:"primaryKey;unique_index;not null;column:id" json:"id" faker:"uuid_hyphenated" ` Name string `gorm:"column:name" json:"name" faker:"name" ` @@ -48,3 +93,79 @@ func (c Connection) String() string { func (c Connection) AsMap(removeFields ...string) map[string]any { return asMap(c, removeFields...) } + +// AsGoGetterURL returns the connection as a url that's supported by https://github.com/hashicorp/go-getter +// Connection details are added to the url as query params +func (c Connection) AsGoGetterURL() (string, error) { + parsedURL, err := url.Parse(c.URL) + if err != nil { + return "", err + } + + var output string + switch c.Type { + case ConnectionTypeHTTP: + if c.Username != "" || c.Password != "" { + parsedURL.User = url.UserPassword(c.Username, c.Password) + } + + output = parsedURL.String() + + case ConnectionTypeGit: + q := parsedURL.Query() + q.Set("sshkey", c.Certificate) + + if v, ok := c.Properties["ref"]; ok { + q.Set("ref", v) + } + + if v, ok := c.Properties["depth"]; ok { + q.Set("depth", v) + } + + parsedURL.RawQuery = q.Encode() + output = parsedURL.String() + + case ConnectionTypeAWS: + q := parsedURL.Query() + q.Set("aws_access_key_id", c.Username) + q.Set("aws_access_key_secret", c.Password) + + if v, ok := c.Properties["profile"]; ok { + q.Set("aws_profile", v) + } + + if v, ok := c.Properties["region"]; ok { + q.Set("region", v) + } + + // For S3 + if v, ok := c.Properties["version"]; ok { + q.Set("version", v) + } + + parsedURL.RawQuery = q.Encode() + output = parsedURL.String() + } + + return output, nil +} + +func (c Connection) AsEnv() []string { + var envs []string + 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)) + + 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)) + } + } + + return envs +}