| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 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 |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | ####################################################################################### |
| 17 | |
| 18 | |
| garciadeblas | 16d1aa2 | 2024-09-16 09:37:20 +0200 | [diff] [blame] | 19 | import base64 |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 20 | import json |
| garciadeblas | 16d1aa2 | 2024-09-16 09:37:20 +0200 | [diff] [blame] | 21 | import yaml |
| 22 | from osm_lcm.lcm_utils import LcmException |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 23 | |
| 24 | |
| 25 | async def create_cloud_credentials(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 26 | self.logger.info( |
| 27 | f"create_cloud_credentials Enter. Operation {op_id}. Params: {op_params}" |
| 28 | ) |
| 29 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 30 | |
| 31 | workflow_template = "launcher-create-providerconfig.j2" |
| 32 | workflow_name = f"create-providerconfig-{content['_id']}" |
| 33 | # vim_name = content["name"].lower() |
| 34 | vim_name = content.get("git_name", content["name"]).lower() |
| 35 | # workflow_name = f"{op_id}-create-credentials-{vim_name}" |
| 36 | |
| 37 | # Test kubectl connection |
| 38 | self.logger.debug(self._kubectl._get_kubectl_version()) |
| 39 | |
| 40 | # Create secret with creds |
| 41 | secret_name = workflow_name |
| 42 | secret_namespace = "osm-workflows" |
| 43 | secret_key = "creds" |
| garciadeblas | 16d1aa2 | 2024-09-16 09:37:20 +0200 | [diff] [blame] | 44 | cloud_config = content.get("config", {}) |
| 45 | if "credentials_base64" in cloud_config: |
| 46 | secret_value = base64.b64decode(cloud_config["credentials_base64"]).decode( |
| 47 | "utf-8" |
| 48 | ) |
| 49 | elif "credentials" in cloud_config: |
| 50 | secret_value = json.dumps(cloud_config["credentials"], indent=2) |
| 51 | else: |
| 52 | raise LcmException("No credentials in VIM/cloud config") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 53 | await self.create_secret( |
| 54 | secret_name, |
| 55 | secret_namespace, |
| 56 | secret_key, |
| 57 | secret_value, |
| 58 | ) |
| 59 | |
| 60 | # Additional params for the workflow |
| 61 | providerconfig_name = f"{vim_name}-config" |
| 62 | provider_type = content["vim_type"] |
| 63 | osm_project_name = "osm_admin" # TODO: get project name from content |
| 64 | if provider_type == "gcp": |
| 65 | vim_tenant = content["vim_tenant_name"] |
| 66 | else: |
| 67 | vim_tenant = "" |
| 68 | |
| 69 | # Render workflow |
| 70 | manifest = self.render_jinja_template( |
| 71 | workflow_template, |
| 72 | output_file=None, |
| 73 | workflow_name=workflow_name, |
| 74 | git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git", |
| 75 | git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git", |
| 76 | providerconfig_name=providerconfig_name, |
| 77 | provider_type=provider_type, |
| 78 | cred_secret_name=vim_name, |
| 79 | temp_cred_secret_name=secret_name, |
| 80 | public_key_mgmt=self._pubkey, |
| 81 | osm_project_name=osm_project_name, |
| 82 | target_gcp_project=vim_tenant, |
| 83 | workflow_debug=self._workflow_debug, |
| 84 | workflow_dry_run=self._workflow_dry_run, |
| 85 | ) |
| 86 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 87 | |
| 88 | # Submit workflow |
| 89 | self._kubectl.create_generic_object( |
| 90 | namespace="osm-workflows", |
| 91 | manifest_dict=yaml.safe_load(manifest), |
| 92 | api_group="argoproj.io", |
| 93 | api_plural="workflows", |
| 94 | api_version="v1alpha1", |
| 95 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 96 | return True, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 97 | |
| 98 | |
| 99 | async def delete_cloud_credentials(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 100 | self.logger.info( |
| 101 | f"delete_cloud_credentials Enter. Operation {op_id}. Params: {op_params}" |
| 102 | ) |
| 103 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 104 | |
| 105 | workflow_template = "launcher-delete-providerconfig.j2" |
| 106 | workflow_name = f"delete-providerconfig-{content['_id']}" |
| 107 | # vim_name = content["name"].lower() |
| 108 | vim_name = content.get("git_name", content["name"]).lower() |
| 109 | # workflow_name = f"{op_id}-delete-credentials-{vim_name}" |
| 110 | |
| 111 | # Additional params for the workflow |
| 112 | providerconfig_name = f"{vim_name}-config" |
| 113 | provider_type = content["vim_type"] |
| 114 | osm_project_name = "osm_admin" # TODO: get project name from content |
| 115 | |
| 116 | # Render workflow |
| 117 | manifest = self.render_jinja_template( |
| 118 | workflow_template, |
| 119 | output_file=None, |
| 120 | workflow_name=workflow_name, |
| 121 | git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git", |
| 122 | git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git", |
| 123 | providerconfig_name=providerconfig_name, |
| 124 | provider_type=provider_type, |
| 125 | osm_project_name=osm_project_name, |
| 126 | workflow_debug=self._workflow_debug, |
| 127 | workflow_dry_run=self._workflow_dry_run, |
| 128 | ) |
| 129 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 130 | |
| 131 | # Submit workflow |
| 132 | self._kubectl.create_generic_object( |
| 133 | namespace="osm-workflows", |
| 134 | manifest_dict=yaml.safe_load(manifest), |
| 135 | api_group="argoproj.io", |
| 136 | api_plural="workflows", |
| 137 | api_version="v1alpha1", |
| 138 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 139 | return True, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 140 | |
| 141 | |
| 142 | async def update_cloud_credentials(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 143 | self.logger.info( |
| 144 | f"update_cloud_credentials Enter. Operation {op_id}. Params: {op_params}" |
| 145 | ) |
| 146 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 147 | |
| 148 | workflow_template = "launcher-update-providerconfig.j2" |
| 149 | workflow_name = f"update-providerconfig-{content['_id']}" |
| 150 | # vim_name = content["name"].lower() |
| 151 | vim_name = content.get("git_name", content["name"]).lower() |
| 152 | # workflow_name = f"{op_id}-update-credentials-{vim_name}" |
| 153 | |
| 154 | # Create secret with creds |
| 155 | secret_name = workflow_name |
| 156 | secret_namespace = "osm-workflows" |
| 157 | secret_key = "creds" |
| garciadeblas | 16d1aa2 | 2024-09-16 09:37:20 +0200 | [diff] [blame] | 158 | cloud_config = content.get("config", {}) |
| 159 | if "credentials_base64" in cloud_config: |
| 160 | secret_value = base64.b64decode(cloud_config["credentials_base64"]).decode( |
| 161 | "utf-8" |
| 162 | ) |
| 163 | elif "credentials" in cloud_config: |
| 164 | secret_value = json.dumps(cloud_config["credentials"], indent=2) |
| 165 | else: |
| 166 | raise LcmException("No credentials in VIM/cloud config") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 167 | await self.create_secret( |
| 168 | secret_name, |
| 169 | secret_namespace, |
| 170 | secret_key, |
| 171 | secret_value, |
| 172 | ) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 173 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 174 | # Additional params for the workflow |
| 175 | providerconfig_name = f"{vim_name}-config" |
| 176 | provider_type = content["vim_type"] |
| 177 | osm_project_name = "osm_admin" # TODO: get project name from content |
| 178 | if provider_type == "gcp": |
| 179 | vim_tenant = content["vim_tenant_name"] |
| 180 | else: |
| 181 | vim_tenant = "" |
| 182 | |
| 183 | # Render workflow |
| 184 | manifest = self.render_jinja_template( |
| 185 | workflow_template, |
| 186 | output_file=None, |
| 187 | workflow_name=workflow_name, |
| 188 | git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git", |
| 189 | git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git", |
| 190 | providerconfig_name=providerconfig_name, |
| 191 | provider_type=provider_type, |
| 192 | cred_secret_name=vim_name, |
| 193 | temp_cred_secret_name=secret_name, |
| 194 | public_key_mgmt=self._pubkey, |
| 195 | osm_project_name=osm_project_name, |
| 196 | target_gcp_project=vim_tenant, |
| 197 | workflow_debug=self._workflow_debug, |
| 198 | workflow_dry_run=self._workflow_dry_run, |
| 199 | ) |
| 200 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 201 | |
| 202 | # Submit workflow |
| 203 | self._kubectl.create_generic_object( |
| 204 | namespace="osm-workflows", |
| 205 | manifest_dict=yaml.safe_load(manifest), |
| 206 | api_group="argoproj.io", |
| 207 | api_plural="workflows", |
| 208 | api_version="v1alpha1", |
| 209 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 210 | return True, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 211 | |
| 212 | |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 213 | async def clean_items_cloud_credentials_create(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 214 | self.logger.info( |
| 215 | f"clean_items_cloud_credentials_create Enter. Operation {op_id}. Params: {op_params}" |
| 216 | ) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 217 | items = { |
| 218 | "secrets": [ |
| 219 | { |
| 220 | "name": f"create-providerconfig-{content['_id']}", |
| 221 | "namespace": "osm-workflows", |
| 222 | } |
| 223 | ] |
| 224 | } |
| 225 | try: |
| 226 | await self.clean_items(items) |
| 227 | return True, "OK" |
| 228 | except Exception as e: |
| 229 | return False, f"Error while cleaning items: {e}" |
| 230 | |
| 231 | |
| 232 | async def clean_items_cloud_credentials_update(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 233 | self.logger.info( |
| 234 | f"clean_items_cloud_credentials_update Enter. Operation {op_id}. Params: {op_params}" |
| 235 | ) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 236 | return await self.clean_items_cloud_credentials_create(op_id, op_params, content) |