From 28f13b65b33fad1c355518273fabc9a64e5c6665 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 | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/models/connections.go b/models/connections.go index 28c09d07..402bf90b 100644 --- a/models/connections.go +++ b/models/connections.go @@ -9,6 +9,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 +92,44 @@ 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: + parsedURL.User = url.UserPassword(c.Username, c.Password) + output = parsedURL.String() + + case ConnectionTypeGit: + q := parsedURL.Query() + q.Add("sshkey", c.Certificate) + + if ref, ok := c.Properties["ref"]; ok { + q.Add("ref", ref) + } + + if depth, ok := c.Properties["depth"]; ok { + q.Add("depth", depth) + } + + parsedURL.RawQuery = q.Encode() + output = parsedURL.String() + + case ConnectionTypeAWS: + q := parsedURL.Query() + q.Add("aws_access_key_id", c.Username) + q.Add("aws_access_key_secret", c.Password) + + parsedURL.RawQuery = q.Encode() + output = parsedURL.String() + } + + return output, nil +}