From 3c644d243fbd1719edb394fddec50c77c5cb3d2f Mon Sep 17 00:00:00 2001 From: khyperia Date: Wed, 10 Jan 2018 15:04:55 -0800 Subject: [PATCH] Create apitype package --- pkg/apitype/clouds.go | 41 ++++++++++++++ pkg/apitype/errors.go | 17 ++++++ pkg/apitype/logs.go | 15 +++++ pkg/apitype/organizations.go | 15 +++++ pkg/apitype/programs.go | 31 +++++++++++ pkg/apitype/stacks.go | 92 +++++++++++++++++++++++++++++++ pkg/apitype/updates.go | 104 +++++++++++++++++++++++++++++++++++ pkg/apitype/users.go | 12 ++++ pkg/backend/cloud/api.go | 2 +- pkg/backend/cloud/backend.go | 2 +- pkg/backend/cloud/stack.go | 2 +- 11 files changed, 330 insertions(+), 3 deletions(-) create mode 100644 pkg/apitype/clouds.go create mode 100644 pkg/apitype/errors.go create mode 100644 pkg/apitype/logs.go create mode 100644 pkg/apitype/organizations.go create mode 100644 pkg/apitype/programs.go create mode 100644 pkg/apitype/stacks.go create mode 100644 pkg/apitype/updates.go create mode 100644 pkg/apitype/users.go diff --git a/pkg/apitype/clouds.go b/pkg/apitype/clouds.go new file mode 100644 index 000000000..e8a87f6ef --- /dev/null +++ b/pkg/apitype/clouds.go @@ -0,0 +1,41 @@ +package apitype + +// Cloud describes a Pulumi Private Cloud (PPC). +type Cloud struct { + Name string `json:"name"` + OrganizationLogin string `json:"organizationLogin"` + Endpoint string `json:"endpoint"` + + // IsDefault flags the Cloud as being the default cloud for new stacks in + // the parent organization. + IsDefault bool `json:"isDefault"` + + StackLimit int `json:"stackLimit"` +} + +// CreateCloudRequest is the request to associate a new Cloud with an organization. +// (The target organization is inferred from the REST URL.) +type CreateCloudRequest struct { + Name string `json:"name"` + Endpoint string `json:"endpoint"` + AccessToken string `json:"accessToken"` +} + +// CloudConfigurationRule is a rule for how the Cloud manages Pulumi program configuration. +type CloudConfigurationRule struct { + Key string `json:"key"` + Type string `json:"type"` + Value string `json:"value"` +} + +// SetCloudConfigurationRequest is the request to set the cloud's configuration. +// It is expected to be a full replacement, not a partial update. +type SetCloudConfigurationRequest struct { + Configuration []CloudConfigurationRule `json:"configuration"` +} + +// CloudStatus describes the state of a Pulumi Private Cloud. +type CloudStatus struct { + Status string `json:"status"` + Versions map[string]string `json:"versions"` +} diff --git a/pkg/apitype/errors.go b/pkg/apitype/errors.go new file mode 100644 index 000000000..03b69f137 --- /dev/null +++ b/pkg/apitype/errors.go @@ -0,0 +1,17 @@ +// Copyright 2016-2017, Pulumi Corporation. All rights reserved. + +package apitype + +import "fmt" + +// ErrorResponse is returned from the API when an actual response body is not appropriate. i.e. +// in all error situations. +type ErrorResponse struct { + Code int `json:"code"` + Message string `json:"message"` +} + +// Error implements the Error interface. +func (err ErrorResponse) Error() string { + return fmt.Sprintf("[%d] %s", err.Code, err.Message) +} diff --git a/pkg/apitype/logs.go b/pkg/apitype/logs.go new file mode 100644 index 000000000..fe187b9ad --- /dev/null +++ b/pkg/apitype/logs.go @@ -0,0 +1,15 @@ +// Copyright 2016-2018, Pulumi Corporation. All rights reserved. + +package apitype + +// LogsResult is the JSON shape of responses to a Logs operation. +type LogsResult struct { + Logs []LogEntry `json:"logs"` +} + +// LogEntry is the individual entries in a JSON response to a Logs operation. +type LogEntry struct { + ID string `json:"id"` + Timestamp int64 `json:"timestamp"` + Message string `json:"message"` +} diff --git a/pkg/apitype/organizations.go b/pkg/apitype/organizations.go new file mode 100644 index 000000000..8a722d987 --- /dev/null +++ b/pkg/apitype/organizations.go @@ -0,0 +1,15 @@ +// Copyright 2016-2017, Pulumi Corporation. All rights reserved. + +package apitype + +// Organization represents a Pulumi organization. +type Organization struct { + GitHubLogin string `json:"githubLogin"` + Name string `json:"name"` + AvatarURL string `json:"avatarUrl"` + + Clouds []Cloud `json:"clouds"` + DefaultCloud string `json:"defaultCloud"` + + Repositories []PulumiRepository `json:"repos"` +} diff --git a/pkg/apitype/programs.go b/pkg/apitype/programs.go new file mode 100644 index 000000000..400c2788d --- /dev/null +++ b/pkg/apitype/programs.go @@ -0,0 +1,31 @@ +package apitype + +// GitHubRepo is a subset of the information returned from the GitHub Repo API. +type GitHubRepo struct { + OwnerLogin string `json:"ownerLogin"` + Name string `json:"name"` + Description string `json:"description"` + IsPrivate bool `json:"isPrivate"` + + HTMLURL string `json:"htmlUrl"` + Homepage string `json:"homepage"` + Topics []string `json:"topics"` +} + +// PulumiRepository is a grouping of "Projects". We also return a subset of the organization's +// GitHub repo with the same name, should it exist. +type PulumiRepository struct { + OrgName string `json:"orgName"` + Name string `json:"name"` + + GitHubRepo *GitHubRepo `json:"githubRepo"` + Projects []PulumiProject `json:"projects"` +} + +// PulumiProject has a 1:1 correspondence to pulumi.yaml files. +type PulumiProject struct { + OrgName string `json:"orgName"` + RepoName string `json:"repoName"` + Name string `json:"name"` + Stacks []string `json:"stacks"` +} diff --git a/pkg/apitype/stacks.go b/pkg/apitype/stacks.go new file mode 100644 index 000000000..ffbed4d16 --- /dev/null +++ b/pkg/apitype/stacks.go @@ -0,0 +1,92 @@ +// Copyright 2016-2018, Pulumi Corporation. All rights reserved. + +package apitype + +import ( + "encoding/json" + + "github.com/pulumi/pulumi/pkg/tokens" +) + +// Resource describes a Cloud resource constructed by Pulumi. +type Resource struct { + Type string `json:"type"` + URN string `json:"urn"` + Custom bool `json:"custom"` + ID string `json:"id"` + Inputs map[string]interface{} `json:"inputs"` + Defaults map[string]interface{} `json:"defaults"` // TODO: extra in pulumi + Outputs map[string]interface{} `json:"outputs"` + Parent string `json:"parent"` + Protect bool `json:"protect"` // TODO: extra in pulumi +} + +// Stack describes a Stack running on a Pulumi Cloud. +type Stack struct { + CloudName string `json:"cloudName"` + OrgName string `json:"orgName"` + + RepoName string `json:"repoName"` + ProjectName string `json:"projName"` + StackName tokens.QName `json:"stackName"` + + ActiveUpdate string `json:"activeUpdate"` + Resources []Resource `json:"resources,omitempty"` + + Version int `json:"version"` +} + +// CreateStackRequest defines the request body for creating a new Stack +type CreateStackRequest struct { + // If empty means use the default cloud. + CloudName string `json:"cloudName"` + // The rest of the StackIdentifier (repo, project) is in the URL. + StackName string `json:"stackName"` +} + +// CreateStackResponse is the response from a create Stack request. +type CreateStackResponse struct { + // The name of the cloud used if the default was sent. + CloudName string `json:"cloudName"` +} + +// EncryptValueRequest defines the request body for encrypting a value. +type EncryptValueRequest struct { + // The value to encrypt. + Plaintext []byte `json:"plaintext"` +} + +// EncryptValueResponse defines the response body for an encrypted value. +type EncryptValueResponse struct { + // The encrypted value. + Ciphertext []byte `json:"ciphertext"` +} + +// DecryptValueRequest defines the request body for decrypting a value. +type DecryptValueRequest struct { + // The value to decrypt. + Ciphertext []byte `json:"ciphertext"` +} + +// DecryptValueResponse defines the response body for a decrypted value. +type DecryptValueResponse struct { + // The decrypted value. + Plaintext []byte `json:"plaintext"` +} + +// StackExport describes an exported stack. +type StackExport struct { + // The opaque Pulumi deployment. + Deployment json.RawMessage `json:"deployment,omitempty"` +} + +// ExportStackResponse defines the response body for exporting a Stack. +type ExportStackResponse StackExport + +// ImportStackRequest defines the request body for importing a Stack. +type ImportStackRequest StackExport + +// ImportStackResponse defines the response body for importing a Stack. +type ImportStackResponse struct { + UpdateID string `json:"updateId"` +} diff --git a/pkg/apitype/updates.go b/pkg/apitype/updates.go new file mode 100644 index 000000000..bd14b3763 --- /dev/null +++ b/pkg/apitype/updates.go @@ -0,0 +1,104 @@ +package apitype + +import "github.com/pulumi/pulumi/pkg/tokens" + +// ConfigValue describes a single (possibly secret) configuration value. +type ConfigValue struct { + // String is either the plaintext value (for non-secrets) or the base64-encoded ciphertext (for secrets). + String string `json:"string"` + // Secret is true if this value is a secret and false otherwise. + Secret bool `json:"secret"` +} + +// UpdateProgramRequest is the request type for updating (aka deploying) a Pulumi program. +type UpdateProgramRequest struct { + // Properties from the Project file. Subset of pack.Package. + Name tokens.PackageName `json:"name"` + Runtime string `json:"runtime"` + Main string `json:"main"` + Description string `json:"description"` + + // Configuration values. + Config map[tokens.ModuleMember]ConfigValue `json:"config"` +} + +/* +// ConfigValue describes a single (possibly secret) configuration value. +type ConfigValue struct { + // String is either the plaintext value (for non-secrets) or the base64-encoded ciphertext (for secrets). + String string `json:"string"` + // Secret is true if this value is a secret and false otherwise. + Secret bool `json:"secret"` +} + +// UpdateProgramRequest is the request type for updating (aka deploying) a Pulumi program. +type UpdateProgramRequest struct { + // Properties from the Project file. + Name string `json:"name"` + Runtime string `json:"runtime"` + Main string `json:"main"` + Description string `json:"description"` + + // Configuration values. Note that although the element type of this map is an `interface{}`, the value must be + // either a string or a ConfigValue. + Config map[string]interface{} `json:"config"` +} +*/ + +// UpdateProgramResponse is the result of an update program request. +type UpdateProgramResponse struct { + // UpdateID is the opaque identifier of the requested update. This value is needed to begin + // an update, as well as poll for its progress. + UpdateID string `json:"updateID"` + + // UploadURL is a URL the client can use to upload their program's contents into. Ignored + // for destroys. + UploadURL string `json:"uploadURL"` +} + +// StartUpdateResponse is the result of the command to start an update. +type StartUpdateResponse struct { + // Version is the version of the program once the update is complete. + // (Will be the current, unchanged value for previews.) + Version int `json:"version"` +} + +// UpdateEventKind is an enum for the type of update events. +type UpdateEventKind string + +const ( + // StdoutEvent is used to mark the event being emitted to STDOUT. + StdoutEvent UpdateEventKind = "stdout" + // StderrEvent is used to mark the event being emitted to STDERR. + StderrEvent UpdateEventKind = "stderr" +) + +// UpdateEvent describes an event that happened on the Pulumi Cloud while processing an update. +type UpdateEvent struct { + Index string `json:"index"` + Kind string `json:"kind"` + Fields map[string]interface{} `json:"fields"` +} + +// UpdateStatus is an enum describing the current state during the lifecycle of an update. +type UpdateStatus string + +const ( + // StatusNotStarted is returned when the Update has been created but not applied. + StatusNotStarted UpdateStatus = "not started" + // StatusRequested is returned when the Update application has been requested but not started. + StatusRequested UpdateStatus = "requested" + // StatusRunning is returned when the Update is in progress. + StatusRunning UpdateStatus = "running" + // StatusFailed is returned when the update has failed. + StatusFailed UpdateStatus = "failed" + // StatusSucceeded is returned when the update has succeeded. + StatusSucceeded UpdateStatus = "succeeded" +) + +// UpdateResults returns a series of events and the current status of an update. The vents can +// be filtered. See API call for more details. +type UpdateResults struct { + Status string `json:"status"` + Events []UpdateEvent `json:"events"` +} diff --git a/pkg/apitype/users.go b/pkg/apitype/users.go new file mode 100644 index 000000000..4b889f3ce --- /dev/null +++ b/pkg/apitype/users.go @@ -0,0 +1,12 @@ +// Copyright 2016-2017, Pulumi Corporation. All rights reserved. + +package apitype + +// User represents a Pulumi user. +type User struct { + ID string `json:"id"` + GitHubLogin string `json:"githubLogin"` + Name string `json:"name"` + AvatarURL string `json:"avatarUrl"` + Organizations []Organization `json:"organizations"` // TODO: This used to be interface{} in pulumi +} diff --git a/pkg/backend/cloud/api.go b/pkg/backend/cloud/api.go index 79cd56b37..2e832f43d 100644 --- a/pkg/backend/cloud/api.go +++ b/pkg/backend/cloud/api.go @@ -15,7 +15,7 @@ import ( "github.com/google/go-querystring/query" "github.com/pkg/errors" - "github.com/pulumi/pulumi/pkg/backend/cloud/apitype" + "github.com/pulumi/pulumi/pkg/apitype" "github.com/pulumi/pulumi/pkg/tokens" "github.com/pulumi/pulumi/pkg/util/contract" "github.com/pulumi/pulumi/pkg/workspace" diff --git a/pkg/backend/cloud/backend.go b/pkg/backend/cloud/backend.go index af4cf3aa4..e0295f7bb 100644 --- a/pkg/backend/cloud/backend.go +++ b/pkg/backend/cloud/backend.go @@ -19,8 +19,8 @@ import ( "github.com/golang/glog" "github.com/pkg/errors" + "github.com/pulumi/pulumi/pkg/apitype" "github.com/pulumi/pulumi/pkg/backend" - "github.com/pulumi/pulumi/pkg/backend/cloud/apitype" "github.com/pulumi/pulumi/pkg/backend/state" "github.com/pulumi/pulumi/pkg/diag" "github.com/pulumi/pulumi/pkg/diag/colors" diff --git a/pkg/backend/cloud/stack.go b/pkg/backend/cloud/stack.go index cd24857e0..eb2dec9e7 100644 --- a/pkg/backend/cloud/stack.go +++ b/pkg/backend/cloud/stack.go @@ -5,8 +5,8 @@ package cloud import ( "encoding/json" + "github.com/pulumi/pulumi/pkg/apitype" "github.com/pulumi/pulumi/pkg/backend" - "github.com/pulumi/pulumi/pkg/backend/cloud/apitype" "github.com/pulumi/pulumi/pkg/engine" "github.com/pulumi/pulumi/pkg/operations" "github.com/pulumi/pulumi/pkg/pack"