1+ #! /bin/bash
2+
3+ # ------------------------------------------------------------
4+ # Copyright 2025 The Radius Authors.
5+ #
6+ # Licensed under the Apache License, Version 2.0 (the "License");
7+ # you may not use this file except in compliance with the License.
8+ # You may obtain a copy of the License at
9+ #
10+ # http://www.apache.org/licenses/LICENSE-2.0
11+ #
12+ # Unless required by applicable law or agreed to in writing, software
13+ # distributed under the License is distributed on an "AS IS" BASIS,
14+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ # See the License for the specific language governing permissions and
16+ # limitations under the License.
17+ # ------------------------------------------------------------
18+
19+ # =============================================================================
20+ # Register a single Radius recipe. Automatically detects whether the recipe
21+ # is Bicep or Terraform and registers it with the appropriate template path.
22+ #
23+ # Usage: ./register-recipe.sh <path-to-recipe-directory>
24+ # Example: ./register-recipe.sh Security/secrets/recipes/kubernetes/bicep
25+ # =============================================================================
26+
27+ set -euo pipefail
28+
29+ RECIPE_PATH=" ${1:- } "
30+
31+ if [[ -z " $RECIPE_PATH " ]]; then
32+ echo " Error: Recipe path is required"
33+ echo " Usage: $0 <path-to-recipe-directory>"
34+ exit 1
35+ fi
36+
37+ if [[ ! -d " $RECIPE_PATH " ]]; then
38+ echo " Error: Recipe directory not found: $RECIPE_PATH "
39+ exit 1
40+ fi
41+
42+ # Normalize path: convert absolute to relative for consistency
43+ RECIPE_PATH=" $( realpath --relative-to=" $( pwd) " " $RECIPE_PATH " 2> /dev/null || echo " $RECIPE_PATH " ) "
44+ RECIPE_PATH=" ${RECIPE_PATH# ./ } "
45+
46+ # Detect recipe type based on file presence
47+ if [[ -f " $RECIPE_PATH /main.tf" ]]; then
48+ RECIPE_TYPE=" terraform"
49+ TEMPLATE_KIND=" terraform"
50+ elif ls " $RECIPE_PATH " /* .bicep & > /dev/null; then
51+ RECIPE_TYPE=" bicep"
52+ TEMPLATE_KIND=" bicep"
53+ else
54+ echo " Error: Could not detect recipe type in $RECIPE_PATH "
55+ exit 1
56+ fi
57+
58+ echo " ==> Registering $RECIPE_TYPE recipe at $RECIPE_PATH "
59+
60+ # Extract resource type from path (e.g., Security/secrets -> Radius.Security/secrets)
61+ RESOURCE_TYPE_PATH=$( echo " $RECIPE_PATH " | sed -E ' s|/recipes/.*||' )
62+ CATEGORY=$( basename " $( dirname " $RESOURCE_TYPE_PATH " ) " )
63+ RESOURCE_NAME=$( basename " $RESOURCE_TYPE_PATH " )
64+ RESOURCE_TYPE=" Radius.$CATEGORY /$RESOURCE_NAME "
65+
66+ echo " ==> Resource type: $RESOURCE_TYPE "
67+
68+ # Determine template path based on recipe type
69+ if [[ " $RECIPE_TYPE " == " bicep" ]]; then
70+ # For Bicep, use OCI registry path (match build-bicep-recipe.sh format)
71+ # Path format: localhost:5000/radius-recipes/{category}/{resourcename}/{platform}/{language}/{recipe-filename}
72+ # Find the .bicep file in the recipe directory
73+ BICEP_FILE=$( ls " $RECIPE_PATH " /* .bicep 2> /dev/null | head -n 1)
74+ RECIPE_FILENAME=$( basename " $BICEP_FILE " .bicep)
75+ RECIPE_NAME=" $RECIPE_FILENAME "
76+
77+ # Extract platform and language from path (e.g., recipes/kubernetes/bicep -> kubernetes/bicep)
78+ RECIPES_SUBPATH=" ${RECIPE_PATH#* recipes/ } "
79+
80+ # Build OCI path (use reciperegistry for in-cluster access)
81+ # Note: Build script pushes to localhost:5000 (which is port-forwarded to reciperegistry)
82+ # But Radius running in-cluster needs to pull from reciperegistry:5000
83+ CATEGORY_LOWER=$( echo " $CATEGORY " | tr ' [:upper:]' ' [:lower:]' )
84+ RESOURCE_LOWER=$( echo " $RESOURCE_NAME " | tr ' [:upper:]' ' [:lower:]' )
85+ TEMPLATE_PATH=" reciperegistry:5000/radius-recipes/${CATEGORY_LOWER} /${RESOURCE_LOWER} /${RECIPES_SUBPATH} /${RECIPE_FILENAME} :latest"
86+ elif [[ " $RECIPE_TYPE " == " terraform" ]]; then
87+ # For Terraform, use HTTP module server with format: resourcename-platform.zip
88+ PLATFORM=$( basename " $( dirname " $RECIPE_PATH " ) " )
89+ RECIPE_NAME=" ${RESOURCE_NAME} -${PLATFORM} "
90+ TEMPLATE_PATH=" http://tf-module-server.radius-test-tf-module-server.svc.cluster.local/${RECIPE_NAME} .zip"
91+ fi
92+
93+ echo " ==> Registering recipe: $RECIPE_NAME "
94+ echo " ==> Template path: $TEMPLATE_PATH "
95+
96+ rad recipe register default \
97+ --environment default \
98+ --resource-type " $RESOURCE_TYPE " \
99+ --template-kind " $TEMPLATE_KIND " \
100+ --template-path " $TEMPLATE_PATH " \
101+ --plain-http
102+
103+ echo " ==> Recipe registered successfully"
0 commit comments