Skip to content

Commit

Permalink
task: remove some ops manager only functions (#2707)
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn authored Mar 5, 2024
1 parent a513e19 commit e1a02aa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 115 deletions.
5 changes: 0 additions & 5 deletions internal/cli/global_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cli

import (
"fmt"
"strings"

"github.com/mongodb/mongodb-atlas-cli/internal/config"
Expand Down Expand Up @@ -72,10 +71,6 @@ func (opts *GlobalOpts) ValidateOrgID() error {
return validate.ObjectID(opts.ConfigOrgID())
}

func DeploymentStatus(baseURL, projectID string) string {
return fmt.Sprintf("Changes are being applied, please check %sv2/%s#deployment/topology for status\n", baseURL, projectID)
}

// GenerateAliases return aliases for use such that they are:
// a version all lower case, a version with dashes, a singular versions with the same rules.
func GenerateAliases(use string, extra ...string) []string {
Expand Down
115 changes: 5 additions & 110 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ package store

import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"

Expand All @@ -38,8 +35,6 @@ import (
)

const (
yes = "yes"
responseHeaderTimeout = 1 * time.Minute
telemetryTimeout = 1 * time.Second
tlsHandshakeTimeout = 5 * time.Second
timeout = 5 * time.Second
Expand Down Expand Up @@ -81,21 +76,6 @@ var defaultTransport = &http.Transport{
ExpectContinueTimeout: expectContinueTimeout,
}

var skipVerifyTransport = &http.Transport{
ResponseHeaderTimeout: responseHeaderTimeout,
TLSHandshakeTimeout: tlsHandshakeTimeout,
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: keepAlive,
}).DialContext,
MaxIdleConns: maxIdleConns,
MaxIdleConnsPerHost: maxIdleConnsPerHost,
Proxy: http.ProxyFromEnvironment,
IdleConnTimeout: idleConnTimeout,
ExpectContinueTimeout: expectContinueTimeout,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec // this is optional for some users,
}

var telemetryTransport = &http.Transport{
DialContext: (&net.Dialer{
Timeout: telemetryTimeout,
Expand All @@ -108,29 +88,6 @@ var telemetryTransport = &http.Transport{
ExpectContinueTimeout: expectContinueTimeout,
}

func customCATransport(ca []byte) *http.Transport {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(ca)
tlsClientConfig := &tls.Config{ //nolint:gosec // we let users set custom certificates
InsecureSkipVerify: false,
RootCAs: caCertPool,
}
return &http.Transport{
ResponseHeaderTimeout: responseHeaderTimeout,
TLSHandshakeTimeout: tlsHandshakeTimeout,
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: keepAlive,
}).DialContext,
MaxIdleConns: maxIdleConns,
MaxIdleConnsPerHost: maxIdleConnsPerHost,
Proxy: http.ProxyFromEnvironment,
IdleConnTimeout: idleConnTimeout,
ExpectContinueTimeout: expectContinueTimeout,
TLSClientConfig: tlsClientConfig,
}
}

func (s *Store) httpClient(httpTransport http.RoundTripper) (*http.Client, error) {
if s.username != "" && s.password != "" {
t := &digest.Transport{
Expand Down Expand Up @@ -162,20 +119,12 @@ func (tr *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
return tr.base.RoundTrip(req)
}

func (s *Store) transport() (*http.Transport, error) {
func (s *Store) transport() *http.Transport {
switch {
case s.caCertificate != "":
dat, err := os.ReadFile(s.caCertificate)
if err != nil {
return nil, err
}
return customCATransport(dat), nil
case s.telemetry:
return telemetryTransport, nil
case s.skipVerify:
return skipVerifyTransport, nil
return telemetryTransport
default:
return defaultTransport, nil
return defaultTransport
}
}

Expand Down Expand Up @@ -360,24 +309,6 @@ response:
return nil
}

// TransportConfigGetter interface for Ops Manager custom network settings.
type TransportConfigGetter interface {
OpsManagerCACertificate() string
OpsManagerSkipVerify() string
}

// NetworkPresets is the default Option to set custom network preference.
func NetworkPresets(c TransportConfigGetter) Option {
options := make([]Option, 0)
if caCertificate := c.OpsManagerCACertificate(); caCertificate != "" {
options = append(options, WithCACertificate(caCertificate))
}
if skipVerify := c.OpsManagerSkipVerify(); skipVerify != yes {
options = append(options, SkipVerify())
}
return Options(options...)
}

// ServiceGetter is a basic interface for service and base url settings.
type ServiceGetter interface {
Service() string
Expand All @@ -387,7 +318,6 @@ type ServiceGetter interface {
// AuthenticatedConfig an interface of the methods needed to set up a Store.
type AuthenticatedConfig interface {
CredentialsGetter
TransportConfigGetter
ServiceGetter
}

Expand All @@ -397,7 +327,6 @@ func AuthenticatedPreset(c AuthenticatedConfig) Option {
if baseURLOpt := baseURLOption(c); baseURLOpt != nil {
options = append(options, baseURLOpt)
}
options = append(options, NetworkPresets(c))
return Options(options...)
}

Expand All @@ -410,18 +339,12 @@ func baseURLOption(c ServiceGetter) Option {
return nil
}

type BasicConfig interface {
TransportConfigGetter
ServiceGetter
}

// UnauthenticatedPreset is the default Option when connecting to the public API without authentication.
func UnauthenticatedPreset(c BasicConfig) Option {
func UnauthenticatedPreset(c ServiceGetter) Option {
options := []Option{Service(c.Service())}
if option := baseURLOption(c); option != nil {
options = append(options, option)
}
options = append(options, NetworkPresets(c))
return Options(options...)
}

Expand All @@ -447,11 +370,7 @@ func New(opts ...Option) (*Store, error) {
}
}

httpTransport, err := store.transport()
if err != nil {
return nil, err
}
client, err := store.httpClient(httpTransport)
client, err := store.httpClient(store.transport())
if err != nil {
return nil, err
}
Expand All @@ -474,27 +393,3 @@ func New(opts ...Option) (*Store, error) {

return store, nil
}

type ManifestGetter interface {
Service() string
OpsManagerVersionManifestURL() string
}

// NewVersionManifest ets the appropriate client for the manifest version page.
func NewVersionManifest(ctx context.Context, c ManifestGetter) (*Store, error) {
s := new(Store)
s.ctx = ctx
s.service = c.Service()
if s.service != config.OpsManagerService {
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
}
s.baseURL = versionManifestStaticPath
if baseURL := c.OpsManagerVersionManifestURL(); baseURL != "" {
s.baseURL = baseURL
}
if err := s.setOpsManagerClient(http.DefaultClient); err != nil {
return nil, err
}

return s, nil
}

0 comments on commit e1a02aa

Please sign in to comment.