|
| 1 | +/* |
| 2 | +Copyright 2023 The Flux CD contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package gitlab |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "github.com/fluxcd/go-git-providers/gitprovider" |
| 25 | + "github.com/xanzy/go-gitlab" |
| 26 | +) |
| 27 | + |
| 28 | +// DeployTokenClient implements the gitprovider.DeployTokenClient interface. |
| 29 | +var _ gitprovider.DeployTokenClient = &DeployTokenClient{} |
| 30 | + |
| 31 | +// DeployTokenClient operates on the access deploy token list for a specific repository. |
| 32 | +type DeployTokenClient struct { |
| 33 | + *clientContext |
| 34 | + ref gitprovider.RepositoryRef |
| 35 | +} |
| 36 | + |
| 37 | +// Get returns the repository at the given path. |
| 38 | +// |
| 39 | +// ErrNotFound is returned if the resource does not exist. |
| 40 | +func (c *DeployTokenClient) Get(_ context.Context, deployTokenName string) (gitprovider.DeployToken, error) { |
| 41 | + return c.get(deployTokenName) |
| 42 | +} |
| 43 | + |
| 44 | +func (c *DeployTokenClient) get(deployTokenName string) (*deployToken, error) { |
| 45 | + deployTokens, err := c.list() |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + // Loop through deploy tokens once we find one with the right name |
| 50 | + for _, dk := range deployTokens { |
| 51 | + if dk.k.Name == deployTokenName { |
| 52 | + return dk, nil |
| 53 | + } |
| 54 | + } |
| 55 | + return nil, gitprovider.ErrNotFound |
| 56 | +} |
| 57 | + |
| 58 | +// List lists all repository deploy tokens of the given deploy token type. |
| 59 | +// |
| 60 | +// List returns all available repository deploy tokens for the given type, |
| 61 | +// using multiple paginated requests if needed. |
| 62 | +func (c *DeployTokenClient) List(_ context.Context) ([]gitprovider.DeployToken, error) { |
| 63 | + dks, err := c.list() |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + // Cast to the generic []gitprovider.DeployToken |
| 68 | + tokens := make([]gitprovider.DeployToken, 0, len(dks)) |
| 69 | + for _, dk := range dks { |
| 70 | + tokens = append(tokens, dk) |
| 71 | + } |
| 72 | + return tokens, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (c *DeployTokenClient) list() ([]*deployToken, error) { |
| 76 | + // GET /repos/{owner}/{repo}/tokens |
| 77 | + apiObjs, err := c.c.ListTokens(getRepoPath(c.ref)) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + // Map the api object to our DeployToken type |
| 83 | + tokens := make([]*deployToken, 0, len(apiObjs)) |
| 84 | + for _, apiObj := range apiObjs { |
| 85 | + // apiObj is already validated at ListTokens |
| 86 | + tokens = append(tokens, newDeployToken(c, apiObj)) |
| 87 | + } |
| 88 | + |
| 89 | + return tokens, nil |
| 90 | +} |
| 91 | + |
| 92 | +// Create creates a deploy token with the given specifications. |
| 93 | +// |
| 94 | +// ErrAlreadyExists will be returned if the resource already exists. |
| 95 | +func (c *DeployTokenClient) Create(_ context.Context, req gitprovider.DeployTokenInfo) (gitprovider.DeployToken, error) { |
| 96 | + apiObj, err := createDeployToken(c.c, c.ref, req) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + return newDeployToken(c, apiObj), nil |
| 101 | +} |
| 102 | + |
| 103 | +// Reconcile makes sure the given desired state (req) becomes the actual state in the backing Git provider. |
| 104 | +// |
| 105 | +// If req doesn't exist under the hood, it is created (actionTaken == true). |
| 106 | +// If req doesn't equal the actual state, the resource will be deleted and recreated (actionTaken == true). |
| 107 | +// If req is already the actual state, this is a no-op (actionTaken == false). |
| 108 | +func (c *DeployTokenClient) Reconcile(ctx context.Context, req gitprovider.DeployTokenInfo) (gitprovider.DeployToken, bool, error) { |
| 109 | + // First thing, validate and default the request to ensure a valid and fully-populated object |
| 110 | + // (to minimize any possible diffs between desired and actual state) |
| 111 | + if err := gitprovider.ValidateAndDefaultInfo(&req); err != nil { |
| 112 | + return nil, false, err |
| 113 | + } |
| 114 | + |
| 115 | + // Get the token with the desired name |
| 116 | + actual, err := c.Get(ctx, req.Name) |
| 117 | + if err != nil { |
| 118 | + // Create if not found |
| 119 | + if errors.Is(err, gitprovider.ErrNotFound) { |
| 120 | + resp, err := c.Create(ctx, req) |
| 121 | + return resp, true, err |
| 122 | + } |
| 123 | + |
| 124 | + // Unexpected path, Get should succeed or return NotFound |
| 125 | + return nil, false, err |
| 126 | + } |
| 127 | + |
| 128 | + actionTaken, err := actual.Reconcile(ctx) |
| 129 | + if err != nil { |
| 130 | + return nil, false, err |
| 131 | + } |
| 132 | + |
| 133 | + return actual, actionTaken, nil |
| 134 | + // |
| 135 | + // // If the desired matches the actual state, just return the actual state |
| 136 | + // if req.Equals(actual.Get()) { |
| 137 | + // return actual, false, nil |
| 138 | + // } |
| 139 | + // |
| 140 | + // // Populate the desired state to the current-actual object |
| 141 | + // if err := actual.Set(req); err != nil { |
| 142 | + // return actual, false, err |
| 143 | + // } |
| 144 | + // // Apply the desired state by running Update |
| 145 | + // return actual, true, actual.Update(ctx) |
| 146 | +} |
| 147 | + |
| 148 | +func createDeployToken(c gitlabClient, ref gitprovider.RepositoryRef, req gitprovider.DeployTokenInfo) (*gitlab.DeployToken, error) { |
| 149 | + // First thing, validate and default the request to ensure a valid and fully-populated object |
| 150 | + // (to minimize any possible diffs between desired and actual state) |
| 151 | + if err := gitprovider.ValidateAndDefaultInfo(&req); err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + |
| 155 | + return c.CreateToken(fmt.Sprintf("%s/%s", ref.GetIdentity(), ref.GetRepository()), deployTokenToAPI(&req)) |
| 156 | +} |
0 commit comments