Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: draft of the basic idea #76

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apis/compute/v1alpha1/droplet_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ type DropletParameters struct {
// +immutable
Tags []string `json:"tags,omitempty"`

// ProjectID: A string specifying the ID of the Project to which the Droplet
// will be assigned.
// +optional
ProjectID *string `json:"project"`

// VPCUUID: A string specifying the UUID of the VPC to which the Droplet
// will be assigned. If excluded, beginning on April 7th, 2020, the Droplet
// will be assigned to your account's default VPC for the region.
Expand Down
5 changes: 5 additions & 0 deletions apis/compute/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 4 additions & 0 deletions package/crds/compute.do.crossplane.io_droplets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ spec:
If no `vpc_uuid` is provided, the Droplet will be placed in
the default VPC.'
type: boolean
project:
description: 'ProjectID: A string specifying the ID of the Project
to which the Droplet will be assigned.'
type: string
region:
description: 'Region: The unique slug identifier for the region
that you wish to deploy in.'
Expand Down
47 changes: 47 additions & 0 deletions pkg/controller/compute/droplet.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,27 @@ func (c *dropletExternal) Observe(ctx context.Context, mg resource.Managed) (man
cr.SetConditions(xpv1.Available())
}

if currentSpec.ProjectID != nil {
page := 0
for {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this doesn't seem specific to droplets, an improvement (especially if doing this for other resources) would be to make this a generic function of the base client so that the individual resource clients can just do something like client.GetProject and return nil if it doesn't exist.

projectResources, _, err := c.Projects.ListResources(ctx, *currentSpec.ProjectID, &godo.ListOptions{Page: page})
if err != nil {
return managed.ExternalObservation{}, err
}
if len(projectResources) == 0 {
return managed.ExternalObservation{
ResourceExists: true,
ResourceUpToDate: false,
}, nil
}
for _, projectResource := range projectResources {
if projectResource.URN == observed.URN() {
break
}
}
}
}

// Droplets are always "up to date" because they can't be updated. ¯\_(ツ)_/¯
return managed.ExternalObservation{
ResourceExists: true,
Expand Down Expand Up @@ -160,6 +181,14 @@ func (c *dropletExternal) Create(ctx context.Context, mg resource.Managed) (mana
ID: droplet.ID,
Status: droplet.Status,
}
currentSpec := cr.Spec.ForProvider.DeepCopy()

if currentSpec.ProjectID != nil {
_, _, err := c.Projects.AssignResources(ctx, *currentSpec.ProjectID, droplet)
if err != nil {
return managed.ExternalCreation{}, err
}
}

if err := c.kube.Status().Update(ctx, cr); err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errDropletUpdate)
Expand All @@ -169,6 +198,24 @@ func (c *dropletExternal) Create(ctx context.Context, mg resource.Managed) (mana
}

func (c *dropletExternal) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) {
cr, ok := mg.(*v1alpha1.Droplet)
if !ok {
return managed.ExternalUpdate{}, errors.New(errNotDroplet)
}
observed, response, err := c.Droplets.Get(ctx, cr.Status.AtProvider.ID)
if err != nil {
return managed.ExternalUpdate{}, errors.Wrap(do.IgnoreNotFound(err, response), errGetDroplet)
}

currentSpec := cr.Spec.ForProvider.DeepCopy()

if currentSpec.ProjectID != nil {
_, _, err := c.Projects.AssignResources(ctx, *currentSpec.ProjectID, observed)
if err != nil {
return managed.ExternalUpdate{}, err
}
}

// Droplets cannot be updated.
return managed.ExternalUpdate{}, nil
}
Expand Down