Skip to content

Commit

Permalink
PoC - Logging, Authentication, generated clients for IAM, Secrets, Co…
Browse files Browse the repository at this point in the history
…smos (#8)

* Add script to generate client with swagger from yaml

* Add generated IAM client(s)

* Add PoC example for how to login and list users

Revert changes in generated code

* Use custom Swagger to avoid having to pass auth info to every method

The underlying `*http.Client` that we provide already sets the header

* Update cluster URL

* Remove hardcoded username/pw, use os.Args instead

* Add User-Agent to DefaultTransport

* Generate Secrets client

* Add example for how to create and retrieve secrets

* Check for conflicts when creating secret

* Add Cosmos client

* Add logging to HTTP client

* Add example to show how installing packaes work

* Fix go vet warning

* Clean up example.org

* Fix linter warnings by adding comments
  • Loading branch information
mrnugget authored and bamarni committed Mar 5, 2019
1 parent 165b2fb commit 9926725
Show file tree
Hide file tree
Showing 238 changed files with 29,714 additions and 6 deletions.
112 changes: 110 additions & 2 deletions dcos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ package dcos
import (
"fmt"
"net/http"
"net/url"

secretsclient "github.com/dcos/client-go/dcos/secrets/client"

cosmosclient "github.com/dcos/client-go/dcos/cosmos/client"

iamclient "github.com/dcos/client-go/dcos/iam/client"
iamlogin "github.com/dcos/client-go/dcos/iam/client/login"
iammodels "github.com/dcos/client-go/dcos/iam/models"

"github.com/go-openapi/runtime"
runtimeClient "github.com/go-openapi/runtime/client"
)

// Client is a client for DC/OS.
type Client struct {
HTTPClient *http.Client
Config *Config
UserAgent string

IAM *iamclient.IdentityAndAccessManagement
Secrets *secretsclient.DCOSSecrets
Cosmos *cosmosclient.Cosmos
}

// NewClient returns a Client which will detect its Config through the well
Expand Down Expand Up @@ -38,9 +53,102 @@ func NewClientWithOptions(httpClient *http.Client, config *Config) (*Client, err
return nil, fmt.Errorf("config cannot be nil")
}

iamClient, err := newIamClient(config.URL(), httpClient)
if err != nil {
return nil, fmt.Errorf("could not create IAM client: %s", err)
}

secretsClient, err := newSecretsClient(config.URL(), httpClient)
if err != nil {
return nil, fmt.Errorf("could not create Secrets client: %s", err)
}

cosmosClient, err := newCosmosClient(config.URL(), httpClient)
if err != nil {
return nil, fmt.Errorf("could not create Cosmos client: %s", err)
}

return &Client{
HTTPClient: httpClient,
Config: config,
UserAgent: fmt.Sprintf("%s(%s)", ClientName, Version),
IAM: iamClient,
Secrets: secretsClient,
Cosmos: cosmosClient,
}, nil
}

// Login uses the IAM client to create a new authentication token for the given
// username and password
func (c *Client) Login(username, password string) (string, error) {
loginObject := &iammodels.LoginObject{UID: username, Password: password}
params := iamlogin.NewPostAuthLoginParams().WithLoginObject(loginObject)

result, err := c.IAM.Login.PostAuthLogin(params)
if err != nil {
return "", err
}

return *result.Payload.Token, nil
}

func newIamClient(clusterURL string, client *http.Client) (*iamclient.IdentityAndAccessManagement, error) {
dcosURL, err := url.Parse(clusterURL)
if err != nil {
return nil, fmt.Errorf("Invalid DC/OS cluster URL '%s': %v", clusterURL, err)
}

iamRuntime := runtimeClient.NewWithClient(
dcosURL.Host,
iamclient.DefaultBasePath,
[]string{dcosURL.Scheme},
client,
)

return iamclient.New(iamRuntime, nil), nil
}

func newSecretsClient(clusterURL string, client *http.Client) (*secretsclient.DCOSSecrets, error) {
dcosURL, err := url.Parse(clusterURL)
if err != nil {
return nil, fmt.Errorf("Invalid DC/OS cluster URL '%s': %v", clusterURL, err)
}

secretsRuntime := runtimeClient.NewWithClient(
dcosURL.Host,
secretsclient.DefaultBasePath,
[]string{dcosURL.Scheme},
client,
)

return secretsclient.New(secretsRuntime, nil), nil
}

func newCosmosClient(clusterURL string, client *http.Client) (*cosmosclient.Cosmos, error) {
dcosURL, err := url.Parse(clusterURL)
if err != nil {
return nil, fmt.Errorf("Invalid DC/OS cluster URL '%s': %v", clusterURL, err)
}

cosmosRuntime := runtimeClient.NewWithClient(
dcosURL.Host,
cosmosclient.DefaultBasePath,
[]string{dcosURL.Scheme},
client,
)

cosmosRuntime.Consumers["application/vnd.dcos.package.install-response+json"] = cosmosRuntime.Consumers[runtime.JSONMime]
cosmosRuntime.Producers["application/vnd.dcos.package.install-request+json;charset=utf-8;version=v1"] = cosmosRuntime.Producers[runtime.JSONMime]
cosmosRuntime.Consumers["application/vnd.dcos.package.uninstall-response+json"] = cosmosRuntime.Consumers[runtime.JSONMime]
cosmosRuntime.Producers["application/vnd.dcos.package.uninstall-request+json;charset=utf-8;version=v1"] = cosmosRuntime.Producers[runtime.JSONMime]
cosmosRuntime.Consumers["application/vnd.dcos.package.error+json"] = cosmosRuntime.Consumers[runtime.JSONMime]
cosmosRuntime.Consumers["application/vnd.dcos.service.update-response+json"] = cosmosRuntime.Consumers[runtime.JSONMime]
cosmosRuntime.Producers["application/vnd.dcos.service.update-request+json;charset=utf-8;version=v1"] = cosmosRuntime.Producers[runtime.JSONMime]
cosmosRuntime.Consumers["application/vnd.dcos.package.repository.add-response+json"] = cosmosRuntime.Consumers[runtime.JSONMime]
cosmosRuntime.Producers["application/vnd.dcos.package.repository.add-request+json;charset=utf-8;version=v1"] = cosmosRuntime.Producers[runtime.JSONMime]
cosmosRuntime.Consumers["application/vnd.dcos.package.repository.delete-response+json"] = cosmosRuntime.Consumers[runtime.JSONMime]
cosmosRuntime.Producers["application/vnd.dcos.package.repository.delete-request+json;charset=utf-8;version=v1"] = cosmosRuntime.Producers[runtime.JSONMime]
cosmosRuntime.Consumers["application/vnd.dcos.service.describe-response+json"] = cosmosRuntime.Consumers[runtime.JSONMime]
cosmosRuntime.Producers["application/vnd.dcos.service.describe-request+json;charset=utf-8;version=v1"] = cosmosRuntime.Producers[runtime.JSONMime]

return cosmosclient.New(cosmosRuntime, nil), nil
}
117 changes: 117 additions & 0 deletions dcos/cosmos/client/cosmos_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 113 additions & 0 deletions dcos/cosmos/client/operations/dcos_system_health_parameters.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9926725

Please sign in to comment.