Skip to content

Commit

Permalink
Ensure at least the machine is allocated in the expected project (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Jun 26, 2023
1 parent b16da33 commit 87afb1c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/resources/metal/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ type MetalService struct {

func New(client metalgo.Client, k8sclient clientset.Interface, projectID string) *MetalService {
machineByUUIDCache := cache.New(time.Minute, func(ctx context.Context, id string) (*models.V1MachineResponse, error) {
machine, err := client.Machine().FindMachine(machine.NewFindMachineParams().WithContext(ctx).WithID(id), nil)
resp, err := client.Machine().FindMachine(machine.NewFindMachineParams().WithContext(ctx).WithID(id), nil)
if err != nil {
return nil, err
}

return machine.Payload, nil
if resp.Payload.Allocation == nil {
return nil, fmt.Errorf("machine %q is not allocated", id)
}
if resp.Payload.Allocation.Project == nil {
return nil, fmt.Errorf("machine %q allocation does not have a project", id)
}
if *resp.Payload.Allocation.Project != projectID {
return nil, fmt.Errorf("machine %q is allocated in the wrong project: %q", id, projectID)
}

return resp.Payload, nil
})
machineByHostnameCache := cache.New(time.Minute, func(ctx context.Context, hostname string) (*models.V1MachineResponse, error) {
resp, err := client.Machine().FindMachines(machine.NewFindMachinesParams().WithContext(ctx).WithBody(&models.V1MachineFindRequest{
Expand Down

0 comments on commit 87afb1c

Please sign in to comment.