diff --git a/go.mod b/go.mod index 7f88d0985..fef14c65c 100644 --- a/go.mod +++ b/go.mod @@ -9,18 +9,18 @@ require ( github.com/Dynatrace/dynatrace-operator v0.14.2 github.com/PagerDuty/go-pagerduty v1.8.0 github.com/andygrunwald/go-jira v1.16.0 - github.com/aws/aws-sdk-go-v2 v1.30.1 + github.com/aws/aws-sdk-go-v2 v1.30.3 github.com/aws/aws-sdk-go-v2/config v1.27.24 github.com/aws/aws-sdk-go-v2/credentials v1.17.24 github.com/aws/aws-sdk-go-v2/service/cloudtrail v1.39.2 github.com/aws/aws-sdk-go-v2/service/costexplorer v1.32.2 - github.com/aws/aws-sdk-go-v2/service/ec2 v1.134.0 + github.com/aws/aws-sdk-go-v2/service/ec2 v1.152.0 github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.20.2 - github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.24.2 + github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0 github.com/aws/aws-sdk-go-v2/service/iam v1.27.2 github.com/aws/aws-sdk-go-v2/service/organizations v1.22.2 github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.18.2 - github.com/aws/aws-sdk-go-v2/service/route53 v1.34.2 + github.com/aws/aws-sdk-go-v2/service/route53 v1.40.3 github.com/aws/aws-sdk-go-v2/service/s3 v1.42.2 github.com/aws/aws-sdk-go-v2/service/servicequotas v1.18.2 github.com/aws/aws-sdk-go-v2/service/sts v1.30.1 @@ -34,6 +34,7 @@ require ( github.com/onsi/ginkgo v1.16.5 github.com/onsi/gomega v1.33.1 github.com/openshift-online/ocm-cli v0.1.74 + github.com/openshift-online/ocm-common v0.0.13 github.com/openshift-online/ocm-sdk-go v0.1.445 github.com/openshift/api v0.0.0-20240524162738-d899f8877d22 github.com/openshift/aws-account-operator/api v0.0.0-20230322125717-5b5a00a3e99f @@ -43,6 +44,7 @@ require ( github.com/openshift/hive/apis v0.0.0-20240216200617-8c54fc9cac45 github.com/openshift/hypershift/api v0.0.0-20241102085541-7ba3433476ee github.com/openshift/osd-network-verifier v1.1.2 + github.com/openshift/rosa v1.2.24 github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 github.com/shopspring/decimal v1.3.1 github.com/sirupsen/logrus v1.9.3 @@ -69,6 +71,8 @@ require ( sigs.k8s.io/yaml v1.4.0 ) +replace github.com/openshift-online/ocm-common v0.0.13 => /Users/kbater/Projects/openshift-online/ocm-common + require ( cloud.google.com/go/compute/metadata v0.3.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect @@ -79,10 +83,10 @@ require ( github.com/Masterminds/semver v1.5.0 // indirect github.com/alessio/shellescape v1.4.2 // indirect github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect - github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.1 // indirect + github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.9 // indirect - github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.13 // indirect - github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.13 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect github.com/aws/aws-sdk-go-v2/internal/v4a v1.2.3 // indirect github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 // indirect diff --git a/go.sum b/go.sum index 1a8d1b547..796254bce 100644 --- a/go.sum +++ b/go.sum @@ -41,20 +41,20 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= github.com/aws/aws-sdk-go v1.44.298 h1:5qTxdubgV7PptZJmp/2qDwD2JL187ePL7VOxsSh1i3g= github.com/aws/aws-sdk-go v1.44.298/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= -github.com/aws/aws-sdk-go-v2 v1.30.1 h1:4y/5Dvfrhd1MxRDD77SrfsDaj8kUkkljU7XE83NPV+o= -github.com/aws/aws-sdk-go-v2 v1.30.1/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc= -github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.1 h1:ZY3108YtBNq96jNZTICHxN1gSBSbnvIdYwwqnvCV4Mc= -github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.1/go.mod h1:t8PYl/6LzdAqsU4/9tz28V/kU+asFePvpOMkdul0gEQ= +github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY= +github.com/aws/aws-sdk-go-v2 v1.30.3/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2/go.mod h1:lPprDr1e6cJdyYeGXnRaJoP4Md+cDBvi2eOj00BlGmg= github.com/aws/aws-sdk-go-v2/config v1.27.24 h1:NM9XicZ5o1CBU/MZaHwFtimRpWx9ohAUAqkG6AqSqPo= github.com/aws/aws-sdk-go-v2/config v1.27.24/go.mod h1:aXzi6QJTuQRVVusAO8/NxpdTeTyr/wRcybdDtfUwJSs= github.com/aws/aws-sdk-go-v2/credentials v1.17.24 h1:YclAsrnb1/GTQNt2nzv+756Iw4mF8AOzcDfweWwwm/M= github.com/aws/aws-sdk-go-v2/credentials v1.17.24/go.mod h1:Hld7tmnAkoBQdTMNYZGzztzKRdA4fCdn9L83LOoigac= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.9 h1:Aznqksmd6Rfv2HQN9cpqIV/lQRMaIpJkLLaJ1ZI76no= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.9/go.mod h1:WQr3MY7AxGNxaqAtsDWn+fBxmd4XvLkzeqQ8P1VM0/w= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.13 h1:5SAoZ4jYpGH4721ZNoS1znQrhOfZinOhc4XuTXx/nVc= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.13/go.mod h1:+rdA6ZLpaSeM7tSg/B0IEDinCIBJGmW8rKDFkYpP04g= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.13 h1:WIijqeaAO7TYFLbhsZmi2rgLEAtWOC1LhxCAVTJlSKw= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.13/go.mod h1:i+kbfa76PQbWw/ULoWnp51EYVWH4ENln76fLQE3lXT8= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 h1:SoNJ4RlFEQEbtDcCEt+QG56MY4fm4W8rYirAmq+/DdU= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15/go.mod h1:U9ke74k1n2bf+RIgoX1SXFed1HLs51OgUSs+Ph0KJP8= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 h1:C6WHdGnTDIYETAm5iErQUiVNsclNx9qbJVPIt03B6bI= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15/go.mod h1:ZQLZqhcu+JhSrA9/NXRm8SkDvsycE+JkV3WGY41e+IM= github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU= github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY= github.com/aws/aws-sdk-go-v2/internal/v4a v1.2.3 h1:lMwCXiWJlrtZot0NJTjbC8G9zl+V3i68gBTBBvDeEXA= @@ -63,12 +63,12 @@ github.com/aws/aws-sdk-go-v2/service/cloudtrail v1.39.2 h1:svl3DNKWpcLOlz+bFzmOx github.com/aws/aws-sdk-go-v2/service/cloudtrail v1.39.2/go.mod h1:gAJs+mKIoK4JTQD1KMZtHgyBRZ8S6Oy5+qjJzoDAvbE= github.com/aws/aws-sdk-go-v2/service/costexplorer v1.32.2 h1:lrOUFVENVFuDcgXu3aXsnpafmS3H6BTZe0TdMw2GOnY= github.com/aws/aws-sdk-go-v2/service/costexplorer v1.32.2/go.mod h1:RhdM96COAaC2AiVCa2QXrxUFOCx0lqK7Tgb6C2PU/5k= -github.com/aws/aws-sdk-go-v2/service/ec2 v1.134.0 h1:ZozGfw2s79TxoqisrkALGCpXokhMkfZRQxPkd8+MK+Y= -github.com/aws/aws-sdk-go-v2/service/ec2 v1.134.0/go.mod h1:xYJZQIo/YZxEbeBxUYRQJTCJ924EuKtDfrhVx76yzOE= +github.com/aws/aws-sdk-go-v2/service/ec2 v1.152.0 h1:ltCQObuImVYmIrMX65ikB9W83MEun3Ry2Sk11ecZ8Xw= +github.com/aws/aws-sdk-go-v2/service/ec2 v1.152.0/go.mod h1:TeZ9dVQzGaLG+SBIgdLIDbJ6WmfFvksLeG3EHGnNfZM= github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.20.2 h1:TIwCp2MbQ+6uxLK4bjA0eAtfOP307sEK3AJCqsXgofQ= github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.20.2/go.mod h1:4UKZnR0ESP4o2BEcfkVb/7uHMg/a8oOqD2W+Pp/oK1c= -github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.24.2 h1:4pOJ+1slB9s36rDsHvnbUd93SZZ4+Z/FdX5f1TKOiQk= -github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.24.2/go.mod h1:NatT0jYQo0MfgZnIX8ReNWnbsl4rbQjuS+uci1KNkck= +github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0 h1:8rDRtPOu3ax8jEctw7G926JQlnFdhZZA4KJzQ+4ks3Q= +github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0/go.mod h1:L5bVuO4PeXuDuMYZfL3IW69E6mz6PDCYpp6IKDlcLMA= github.com/aws/aws-sdk-go-v2/service/iam v1.27.2 h1:Z3a5I5kKGsuVW4kbrtHVnLGUHpEpo19zFyo6dzP2WCM= github.com/aws/aws-sdk-go-v2/service/iam v1.27.2/go.mod h1:CYRyr95Q57xVvrcKJu3vw4jVVCZhmY1SyugM+EWXlzI= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 h1:dT3MqvGhSoaIhRseqw2I0yH81l7wiR2vjs57O51EAm8= @@ -83,8 +83,8 @@ github.com/aws/aws-sdk-go-v2/service/organizations v1.22.2 h1:8MvbXgYQVyXMxKAxXt github.com/aws/aws-sdk-go-v2/service/organizations v1.22.2/go.mod h1:+9jQMB3NSsJDnETdNbkjwnLmOV6+mUjSUIjqims7eIM= github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.18.2 h1:ZsDgaGL2WyRlhHtRk7DcFR8k/nq0V/+I7c3iA2r7Z9Y= github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.18.2/go.mod h1:Gm3DRdZ1aj2aJea5fA8dmwtsn2goY4WVU0sMLYdY/MM= -github.com/aws/aws-sdk-go-v2/service/route53 v1.34.2 h1:h9I/GZ7POL9EOyFouZi7P3v0G8Sr/nMIo9uHIyk+NSU= -github.com/aws/aws-sdk-go-v2/service/route53 v1.34.2/go.mod h1:adrwIPgzeyWkAyIlnH7SkTU/UTATCXncCR4fbxwYLhA= +github.com/aws/aws-sdk-go-v2/service/route53 v1.40.3 h1:wr5gulbwbb8PSRMWjCROoP0TIMccpF8x5A7hEk2SjpA= +github.com/aws/aws-sdk-go-v2/service/route53 v1.40.3/go.mod h1:/Gyl9xjGcjIVe80ar75YlmA8m6oFh0A4XfLciBmdS8s= github.com/aws/aws-sdk-go-v2/service/s3 v1.42.2 h1:NnduxUd9+Fq9DcCDdJK8v6l9lR1xDX4usvog+JuQAno= github.com/aws/aws-sdk-go-v2/service/s3 v1.42.2/go.mod h1:NXRKkiRF+erX2hnybnVU660cYT5/KChRD4iUgJ97cI8= github.com/aws/aws-sdk-go-v2/service/servicequotas v1.18.2 h1:VZCExgKV9+kbNnpZhV4kT8yFJtZ2PuSoTCYN0rHWrMk= @@ -471,6 +471,8 @@ github.com/openshift/hypershift/api v0.0.0-20241102085541-7ba3433476ee h1:KcTMIN github.com/openshift/hypershift/api v0.0.0-20241102085541-7ba3433476ee/go.mod h1:NIT2Bs83re4seKsT3Xp+ENOOCN2Gl++mguuGGhNnN/8= github.com/openshift/osd-network-verifier v1.1.2 h1:iF8Wk6W5Q/fNFQi4h8mOF/rsLpgmI1y7WNKimv+jqJk= github.com/openshift/osd-network-verifier v1.1.2/go.mod h1:X3dVNkC91NYTf2kTXUS/PeRTNvfS97WbvIqPIDP083M= +github.com/openshift/rosa v1.2.24 h1:vv0yYnWHx6CCPEAau/0rS54P2ksaf+uWXb1TQPWxiYE= +github.com/openshift/rosa v1.2.24/go.mod h1:MVXB27O3PF8WoOic23I03mmq6/9kVxpFx6FKyLMCyrQ= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/perimeterx/marshmallow v1.1.4 h1:pZLDH9RjlLGGorbXhcaQLhfuV0pFMNfPO55FuFkxqLw= diff --git a/pkg/utils/ocm.go b/pkg/utils/ocm.go index f7c4dc117..299bd49e4 100644 --- a/pkg/utils/ocm.go +++ b/pkg/utils/ocm.go @@ -2,16 +2,16 @@ package utils import ( "encoding/json" - "errors" "fmt" "log" "os" - "path/filepath" "regexp" "strings" "github.com/aws/aws-sdk-go-v2/aws/arn" "github.com/google/uuid" + conn "github.com/openshift-online/ocm-common/pkg/ocm/connection-builder" + "github.com/openshift-online/ocm-common/pkg/ocm/consts" sdk "github.com/openshift-online/ocm-sdk-go" cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" ) @@ -19,48 +19,10 @@ import ( const ClusterServiceClusterSearch = "id = '%s' or name = '%s' or external_id = '%s'" const ( - productionURL = "https://api.openshift.com" - stagingURL = "https://api.stage.openshift.com" - integrationURL = "https://api.integration.openshift.com" - productionGovURL = "https://api-admin.openshiftusgov.com" HypershiftClusterTypeLabel = "ext-hypershift.openshift.io/cluster-type" DynatraceTenantKeyLabel = "sre-capabilities.dtp.tenant" ) -var urlAliases = map[string]string{ - "production": productionURL, - "prod": productionURL, - "prd": productionURL, - productionURL: productionURL, - "staging": stagingURL, - "stage": stagingURL, - "stg": stagingURL, - stagingURL: stagingURL, - "integration": integrationURL, - "int": integrationURL, - integrationURL: integrationURL, - "productiongov": productionGovURL, - "prodgov": productionGovURL, - "prdgov": productionGovURL, - productionGovURL: productionGovURL, -} - -// Config describes the OCM client configuration -// Taken wholesale from openshift-online/ocm-cli -type Config struct { - AccessToken string `json:"access_token,omitempty" doc:"Bearer access token."` - ClientID string `json:"client_id,omitempty" doc:"OpenID client identifier."` - ClientSecret string `json:"client_secret,omitempty" doc:"OpenID client secret."` - Insecure bool `json:"insecure,omitempty" doc:"Enables insecure communication with the server. This disables verification of TLS certificates and host names."` - Password string `json:"password,omitempty" doc:"User password."` - RefreshToken string `json:"refresh_token,omitempty" doc:"Offline or refresh token."` - Scopes []string `json:"scopes,omitempty" doc:"OpenID scope. If this option is used it will replace completely the default scopes. Can be repeated multiple times to specify multiple scopes."` - TokenURL string `json:"token_url,omitempty" doc:"OpenID token URL."` - URL string `json:"url,omitempty" doc:"URL of the API gateway. The value can be the complete URL or an alias. The valid aliases are 'production', 'staging' and 'integration'."` - User string `json:"user,omitempty" doc:"User name."` - Pager string `json:"pager,omitempty" doc:"Pager command, for example 'less'. If empty no pager will be used."` -} - // GetClusterAnyStatus returns an OCM cluster object given an OCM connection and cluster id // (internal id, external id, and name all supported). func GetClusterAnyStatus(conn *sdk.Connection, clusterId string) (*cmv1.Cluster, error) { @@ -157,146 +119,16 @@ func GenerateQuery(clusterIdentifier string) string { } } -// Finds the OCM Configuration file and returns the path to it -// Taken wholesale from openshift-online/ocm-cli -func getOCMConfigLocation() (string, error) { - if ocmconfig := os.Getenv("OCM_CONFIG"); ocmconfig != "" { - return ocmconfig, nil - } - - // Determine home directory to use for the legacy file path - home, err := os.UserHomeDir() - if err != nil { - return "", err - } - - path := filepath.Join(home, ".ocm.json") - - _, err = os.Stat(path) - if os.IsNotExist(err) { - // Determine standard config directory - configDir, err := os.UserConfigDir() - if err != nil { - return path, err - } - - // Use standard config directory - path = filepath.Join(configDir, "/ocm/ocm.json") - } - - return path, nil -} - -// Loads the OCM Configuration file -// Taken wholesale from openshift-online/ocm-cli -func loadOCMConfig() (*Config, error) { - var err error - - file, err := getOCMConfigLocation() - if err != nil { - return nil, err - } - - _, err = os.Stat(file) - if os.IsNotExist(err) { - cfg := &Config{} - err = nil - return cfg, err - } - - if err != nil { - err = fmt.Errorf("can't check if config file '%s' exists: %v", file, err) - return nil, err - } - - data, err := os.ReadFile(file) - if err != nil { - err = fmt.Errorf("can't read config file '%s': %v", file, err) - return nil, err - } - - if len(data) == 0 { - return nil, nil - } - - cfg := &Config{} - err = json.Unmarshal(data, cfg) - - if err != nil { - err = fmt.Errorf("can't parse config file '%s': %v", file, err) - return cfg, err - } - - return cfg, nil -} - -func getOcmConfiguration(ocmConfigLoader func() (*Config, error)) (*Config, error) { - tokenEnv := os.Getenv("OCM_TOKEN") - urlEnv := os.Getenv("OCM_URL") - refreshTokenEnv := os.Getenv("OCM_REFRESH_TOKEN") // Unlikely to be set, but check anyway - - config := &Config{} - - // If missing required data, load from the config file. - // We don't want to always load this, because the user might only use environment variables. - if tokenEnv == "" || refreshTokenEnv == "" || urlEnv == "" { - var fileConfigLoadError error - config, fileConfigLoadError = ocmConfigLoader() - if fileConfigLoadError != nil { - return config, fmt.Errorf("could not load OCM configuration file") - } - } - - // Overwrite with set environment variables, to allow users to overwrite - // their configuration file's variables - if tokenEnv != "" { - config.AccessToken = tokenEnv - } - if urlEnv != "" { - config.URL = urlEnv - } - if refreshTokenEnv != "" { - config.RefreshToken = refreshTokenEnv - } - - return config, nil -} - func CreateConnection() (*sdk.Connection, error) { - ocmConfigError := "Unable to load OCM config\nLogin with 'ocm login' or set OCM_TOKEN, OCM_URL and OCM_REFRESH_TOKEN environment variables" - - connectionBuilder := sdk.NewConnectionBuilder() - - config, err := getOcmConfiguration(loadOCMConfig) - if err != nil { - return nil, errors.New(ocmConfigError) - } - - connectionBuilder.Tokens(config.AccessToken, config.RefreshToken) - - if config.URL == "" { - return nil, errors.New(ocmConfigError) - } - - // Parse the URL in case it is an alias - gatewayURL, ok := urlAliases[config.URL] - if !ok { - return nil, fmt.Errorf("invalid OCM_URL found: %s\nValid URL aliases are: 'production', 'staging', 'integration'", config.URL) - } - connectionBuilder.URL(gatewayURL) + connection := conn.NewConnection() + connection = connection.AsAgent("osdctl/" + Version) - connectionBuilder.Client(config.ClientID, config.ClientSecret) - - connection, err := connectionBuilder.Build() - - if err != nil { - if strings.Contains(err.Error(), "Not logged in, run the") { - return nil, errors.New(ocmConfigError) - } - return nil, fmt.Errorf("failed to create OCM connection: %v", err) + // overwrite the config URL if the environment variable is set + if overrideUrl := os.Getenv(consts.URLEnvKey); overrideUrl != "" { + connection = connection.WithApiUrl(overrideUrl) } - return connection, nil + return connection.Build() } func GetSupportRoleArnForCluster(ocmClient *sdk.Connection, clusterID string) (string, error) { diff --git a/pkg/utils/ocm_test.go b/pkg/utils/ocm_test.go index 2755e6c9d..a483f117f 100644 --- a/pkg/utils/ocm_test.go +++ b/pkg/utils/ocm_test.go @@ -1,139 +1,9 @@ package utils import ( - "os" "testing" ) -func resetEnvVars(t *testing.T) { - errToken := os.Unsetenv("OCM_TOKEN") - errUrl := os.Unsetenv("OCM_URL") - errRefreshToken := os.Unsetenv("OCM_REFRESH_TOKEN") - if errToken != nil || errUrl != nil || errRefreshToken != nil { - t.Fatal("Error setting environment variables") - } -} - -func assertConfigValues(t *testing.T, config *Config, err error, expectedUrl string, expectedToken string, expectedRefreshToken string) { - if err != nil { - t.Errorf("Count not read configuration %q", err) - } - if config.URL != expectedUrl { - t.Fatalf( - "Expected config URL, %s, does not match the actual, %s.", - expectedUrl, - config.URL, - ) - } - if config.AccessToken != expectedToken { - t.Errorf( - "Expected config access token, %s, does not match the actual, %s.", - expectedToken, - config.AccessToken, - ) - } - if config.RefreshToken != expectedRefreshToken { - t.Errorf( - "Expected config refresh token, %s, does not match the actual, %s.", - expectedRefreshToken, - config.RefreshToken, - ) - } -} - -func TestGetOCMConfigLocationWithEnvSet(t *testing.T) { - resetEnvVars(t) - defer resetEnvVars(t) - - expectedConfigLocation := "~/.config/ocm/ocm.test.json" - envConfigErr := os.Setenv("OCM_CONFIG", expectedConfigLocation) - if envConfigErr != nil { - t.Fatal("Error setting OCM_CONFIG") - } - actualConfigLocation, err := getOCMConfigLocation() - if err != nil { - t.Errorf("Error getting OCM config location %q", err) - } - if actualConfigLocation != expectedConfigLocation { - t.Errorf( - "Expected location, %s, did not match actual location, %s.", - expectedConfigLocation, - actualConfigLocation, - ) - } -} - -func TestGetOCMConfigurationWithNoEnvVarsSet(t *testing.T) { - resetEnvVars(t) - defer resetEnvVars(t) - - expectedToken := "asdf" - expectedUrl := "https://example.com" - expectedRefreshToken := "fdsa" - config, err := getOcmConfiguration(func() (*Config, error) { - return &Config{ - URL: expectedUrl, - AccessToken: expectedToken, - RefreshToken: expectedRefreshToken, - }, nil - }) - if err != nil { - t.Errorf("Count not read configuration %q", err) - } - - assertConfigValues(t, config, err, expectedUrl, expectedToken, expectedRefreshToken) -} - -func TestGetOCMConfigurationTokenAndUrlEnvVarsSet(t *testing.T) { - resetEnvVars(t) - defer resetEnvVars(t) - - expectedToken := "asdf" - expectedUrl := "https://example.com" - expectedRefreshToken := "fdsa" - errToken := os.Setenv("OCM_TOKEN", expectedToken) - errUrl := os.Setenv("OCM_URL", expectedUrl) - if errToken != nil || errUrl != nil { - t.Error("Error setting environment variables") - } - config, err := getOcmConfiguration(func() (*Config, error) { - return &Config{ - URL: "https://fail.example.com", - AccessToken: "fail", - RefreshToken: expectedRefreshToken, - }, nil - }) - if err != nil { - t.Errorf("Count not read configuration %q", err) - } - - assertConfigValues(t, config, err, expectedUrl, expectedToken, expectedRefreshToken) -} - -func TestGetOCMConfigurationTokenAndUrlAndRefreshTokenEnvVarsSet(t *testing.T) { - resetEnvVars(t) - defer resetEnvVars(t) - - expectedToken := "asdf" - expectedUrl := "https://example.com" - expectedRefreshToken := "fdsa" - errToken := os.Setenv("OCM_TOKEN", expectedToken) - errUrl := os.Setenv("OCM_URL", expectedUrl) - errRefreshToken := os.Setenv("OCM_REFRESH_TOKEN", expectedRefreshToken) - if errToken != nil || errUrl != nil || errRefreshToken != nil { - t.Error("Error setting environment variables") - } - config, err := getOcmConfiguration(func() (*Config, error) { - return &Config{ - URL: "https://fail.example.com", - AccessToken: "fail", - RefreshToken: "fail", - }, nil - }) - - assertConfigValues(t, config, err, expectedUrl, expectedToken, expectedRefreshToken) -} - func TestGenerateQuery(t *testing.T) { tests := []struct { name string