automation

package
v0.0.0-...-02be50d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2020 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(service GoogleService, recommendation *gcloudRecommendation, task *Task) error

Apply is the method used to apply recommendations from Recommender API. Supports recommendations from the following recommenders: - google.compute.disk.IdleResourceRecommender - google.compute.instance.IdleResourceRecommender - google.compute.instance.MachineTypeRecommender

func ApplyByName

func ApplyByName(service GoogleService, recommendationName string, task *Task) error

ApplyByName gets the recommendation by name and applies the recommendation using the Apply function.

func AwaitCompletion

func AwaitCompletion(gen operationGenerator, sleepTime time.Duration) error

AwaitCompletion takes a function that needs to be called repeatedly to check if a process (some Google Service request) has finished. Such a function is usually constructed by wrapping a requestId(x).Do() call.

func DoOperation

func DoOperation(service GoogleService, operation *gcloudOperation) error

DoOperation does the action specified in the operation.

func DoOperations

func DoOperations(service GoogleService, recommendation *gcloudRecommendation, task *Task) error

DoOperations calls DoOperation for each operation specified in the recommendation

func DoRequestWithRetries

func DoRequestWithRetries(call apiCall) error

DoRequestWithRetries calls the specified function while it returns error with error code listed in httpStatusesToRetry, and tries again after some time. Stops, when maxSleepTime is reached or code not in in httpStatusesToRetry. Returns the last received result from apiCall.

func ListLocations

func ListLocations(service GoogleService, project string) ([]string, error)

ListLocations return the list of all locations per project(zones and regions). Exactly one of returned values will be non-nil.

func ListRecommendations

func ListRecommendations(service GoogleService, project string, numConcurrentCalls int, task *Task) ([]*gcloudRecommendation, error)

ListRecommendations returns the list of recommendations for a Cloud project from googleRecommenders. Requires the recommender.*.list IAM permissions for the recommenders. numConcurrentCalls specifies the maximum number of concurrent calls to ListRecommendations method, non-positive values are ignored, instead the default value is used. task structure tracks the progress of the function.

Types

type GoogleService

type GoogleService interface {
	// changes the machine type of an instance
	ChangeMachineType(project, zone, instance, machineType string) error

	// creates a snapshot of a disk
	CreateSnapshot(project, zone, disk, name string) error

	// deletes persistent disk
	DeleteDisk(project, zone, disk string) error

	// gets the specified instance resource
	GetInstance(project string, zone string, instance string) (*compute.Instance, error)

	// gets recommendation by name
	GetRecommendation(name string) (*gcloudRecommendation, error)

	// lists whether the requirements have been met for all APIs (APIs enabled).
	ListAPIRequirements(project string, apis []string) ([]*Requirement, error)

	// lists whether the requirements have been met for all required permissions.
	ListPermissionRequirements(project string, permissions [][]string) ([]*Requirement, error)

	// lists projects
	ListProjects() ([]string, error)

	// listing recommendations for specified project, zone and recommender
	ListRecommendations(project, location, recommenderID string) ([]*gcloudRecommendation, error)

	// listing every zone available for the project methods
	ListZonesNames(project string) ([]string, error)

	// listing every region available for the project methods
	ListRegionsNames(project string) ([]string, error)

	// marks recommendation for the project with given etag and name claimed
	MarkRecommendationClaimed(name, etag string) (*gcloudRecommendation, error)

	// marks recommendation for the project with given etag and name succeeded
	MarkRecommendationSucceeded(name, etag string) (*gcloudRecommendation, error)

	// marks recommendation for the project with given etag and name failed
	MarkRecommendationFailed(name, etag string) (*gcloudRecommendation, error)

	// stops the specified instance
	StopInstance(project, zone, instance string) error

	// starts the specified instance
	StartInstance(project, zone, instance string) error
}

GoogleService is the inferface that prodives methods required to list recommendations and apply them

func NewGoogleService

func NewGoogleService(ctx context.Context, conf *oauth2.Config, tok *oauth2.Token) (GoogleService, error)

NewGoogleService creates new googleServices. If creation failed the error will be non-nil.

type ListResult

type ListResult struct {
	Recommendations []*gcloudRecommendation
	FailedProjects  []*ProjectRequirements
}

ListResult contains information about listing recommendations for all projects. If user doesn't have enough permissions for the project, the requirements, including failed ones, are listed in failedProjects. Otherwise, recommendations for the project are appended to recommendations.

func ListProjectsRecommendations

func ListProjectsRecommendations(service GoogleService, projects []string, numConcurrentCalls int, task *Task) (*ListResult, error)

ListProjectsRecommendations gets recommendations for the specified projects. If the user has enough permissions to apply and list recommendations, recommendations for project are listed. Otherwise, projects requirements, including failed ones, are added to `failedProjects` to help show warnings to the user. task structure tracks how many subtasks have been done already.

type ProjectRequirements

type ProjectRequirements struct {
	Project      string         `json:"project"`
	Requirements []*Requirement `json:"requirements"`
}

ProjectRequirements contains information about permissions for the user for the project.

func ListRequirements

func ListRequirements(s GoogleService, projects []string, task *Task) ([]*ProjectRequirements, error)

ListRequirements lists the requirements and their statuses for every project. task structure tracks how many projects have been processed already.

type Requirement

type Requirement struct {
	Name         string `json:"name"`
	Satisfied    bool   `json:"satisfied"`
	ErrorMessage string `json:"errorMessage"`
}

Requirement contains information about the required permission or api. Satisfied states whether permission is given and API is enabled. If Satisfied if false, ErrorMessage will contain information about the problem.

func ListProjectRequirements

func ListProjectRequirements(s GoogleService, project string) ([]*Requirement, error)

ListProjectRequirements is a function that lists all permissions and APIs and their statuses for a project. If all statuses are equal to RequirementCompleted, user has all required permissions.

type Task

type Task struct {
	// contains filtered or unexported fields
}

Task is the structure that helps get the percentage of work done. GetProgress method is thread-safe. IncrementDone and GetNextSubtask is thread-safe, but GetProgress is calculated in assumption that all subtasks, except lower-level subtasks with no subtasks, will be done consequently.

func (*Task) GetNextSubtask

func (t *Task) GetNextSubtask() *Task

GetNextSubtask returns the pointer to next not done subtask

func (*Task) GetProgress

func (t *Task) GetProgress() (int32, int32)

GetProgress returns the fraction of work done. Assumes that all subtasks, except the subtasks, that don't have their own subtasks are done subseequently Returned values are numerator and denominator of that fraction. If not all work is done numerator is garantueed to be less than denominator.

func (*Task) IncrementDone

func (t *Task) IncrementDone()

IncrementDone increments subtasksDone

func (*Task) SetAllDone

func (t *Task) SetAllDone()

SetAllDone sets the task done

func (*Task) SetNumberOfSubtasks

func (t *Task) SetNumberOfSubtasks(num int)

SetNumberOfSubtasks is used to set number of subtasks in the task

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL