|
| 1 | +/*This file is part of kuberpult. |
| 2 | +
|
| 3 | +Kuberpult is free software: you can redistribute it and/or modify |
| 4 | +it under the terms of the Expat(MIT) License as published by |
| 5 | +the Free Software Foundation. |
| 6 | +
|
| 7 | +Kuberpult is distributed in the hope that it will be useful, |
| 8 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | +MIT License for more details. |
| 11 | +
|
| 12 | +You should have received a copy of the MIT License |
| 13 | +along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>. |
| 14 | +
|
| 15 | +Copyright freiheit.com*/ |
| 16 | +/* |
| 17 | +This file is part of kuberpult. |
| 18 | +
|
| 19 | +Kuberpult is free software: you can redistribute it and/or modify |
| 20 | +it under the terms of the Expat(MIT) License as published by |
| 21 | +the Free Software Foundation. |
| 22 | +
|
| 23 | +Kuberpult is distributed in the hope that it will be useful, |
| 24 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | +MIT License for more details. |
| 27 | +
|
| 28 | +You should have received a copy of the MIT License |
| 29 | +along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>. |
| 30 | +
|
| 31 | +Copyright freiheit.com |
| 32 | +*/ |
| 33 | +package environments |
| 34 | + |
| 35 | +import ( |
| 36 | + "fmt" |
| 37 | + "net/http" |
| 38 | + urllib "net/url" |
| 39 | + |
| 40 | + "github.com/freiheit-com/kuberpult/cli/pkg/cli_utils" |
| 41 | + kutil "github.com/freiheit-com/kuberpult/cli/pkg/kuberpult_utils" |
| 42 | +) |
| 43 | + |
| 44 | +type DeleteEnvironmentParameters struct { |
| 45 | + Environment string |
| 46 | +} |
| 47 | + |
| 48 | +func HandleDeleteEnvironment(requestParams kutil.RequestParameters, authParams kutil.AuthenticationParameters, params *DeleteEnvironmentParameters) error { |
| 49 | + req, err := createHttpRequest(*requestParams.Url, authParams, params) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("error while preparing HTTP request, error: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + if err = cli_utils.IssueHttpRequest(*req, requestParams.Retries, requestParams.HttpTimeout); err != nil { |
| 55 | + return fmt.Errorf("error while issuing HTTP request, error: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +func createHttpRequest(url string, authParams kutil.AuthenticationParameters, parameters *DeleteEnvironmentParameters) (*http.Request, error) { |
| 62 | + urlStruct, err := urllib.Parse(url) |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("the provided url %s is invalid, error: %w", url, err) |
| 65 | + } |
| 66 | + |
| 67 | + path := "/api/environments/" + parameters.Environment |
| 68 | + |
| 69 | + req, err := http.NewRequest(http.MethodDelete, urlStruct.JoinPath(path).String(), nil) |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("error creating the HTTP request, error: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + if authParams.IapToken != nil { |
| 75 | + req.Header.Add("Proxy-Authorization", "Bearer "+*authParams.IapToken) |
| 76 | + } |
| 77 | + |
| 78 | + if authParams.DexToken != nil { |
| 79 | + req.Header.Add("Authorization", "Bearer "+*authParams.DexToken) |
| 80 | + } |
| 81 | + |
| 82 | + return req, nil |
| 83 | +} |
0 commit comments