Skip to content

Commit

Permalink
feat: add AsGoGetterURL() to connection
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Oct 18, 2023
1 parent 8cce7a5 commit 28f13b6
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions models/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" `
Expand Down Expand Up @@ -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
}

0 comments on commit 28f13b6

Please sign in to comment.