|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package ack |
| 15 | + |
| 16 | +import ( |
| 17 | + "bytes" |
| 18 | + "fmt" |
| 19 | + "io/ioutil" |
| 20 | + "path/filepath" |
| 21 | + ttpl "text/template" |
| 22 | + |
| 23 | + ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model" |
| 24 | + ackutil "github.com/aws-controllers-k8s/code-generator/pkg/util" |
| 25 | +) |
| 26 | + |
| 27 | +/* |
| 28 | +The following hook points are supported in the ACK controller resource manager |
| 29 | +code paths: |
| 30 | +
|
| 31 | +* sdk_read_one_pre_build_request |
| 32 | +* sdk_create_pre_build_request |
| 33 | +* sdk_update_pre_build_request |
| 34 | +* sdk_delete_pre_build_request |
| 35 | +* sdk_read_one_post_request |
| 36 | +* sdk_create_post_request |
| 37 | +* sdk_update_post_request |
| 38 | +* sdk_delete_post_request |
| 39 | +* sdk_read_one_pre_set_output |
| 40 | +* sdk_create_pre_set_output |
| 41 | +* sdk_update_pre_set_output |
| 42 | +
|
| 43 | +The "pre_build_request" hooks are called BEFORE the call to construct |
| 44 | +the Input shape that is used in the API operation and therefore BEFORE |
| 45 | +any call to validate that Input shape. |
| 46 | +
|
| 47 | +The "post_request" hooks are called IMMEDIATELY AFTER the API operation |
| 48 | +aws-sdk-go client call. These hooks will have access to a Go variable |
| 49 | +named `resp` that refers to the aws-sdk-go client response and a Go |
| 50 | +variable named `respErr` that refers to any error returned from the |
| 51 | +aws-sdk-go client call. |
| 52 | +
|
| 53 | +The "pre_set_output" hooks are called BEFORE the code that processes the |
| 54 | +Outputshape (the pkg/generate/code.SetOutput function). These hooks will |
| 55 | +have access to a Go variable named `ko` that represents the concrete |
| 56 | +Kubernetes CR object that will be returned from the main method |
| 57 | +(sdkFind, sdkCreate, etc). This `ko` variable will have been defined |
| 58 | +immediately before the "pre_set_output" hooks as a copy of the resource |
| 59 | +that is supplied to the main method, like so: |
| 60 | +
|
| 61 | +```go |
| 62 | + // Merge in the information we read from the API call above to the copy of |
| 63 | + // the original Kubernetes object we passed to the function |
| 64 | + ko := r.ko.DeepCopy() |
| 65 | +``` |
| 66 | +*/ |
| 67 | + |
| 68 | +// ResourceHookCode returns a string with custom callback code for a resource |
| 69 | +// and hook identifier |
| 70 | +func ResourceHookCode( |
| 71 | + templateBasePaths []string, |
| 72 | + r *ackmodel.CRD, |
| 73 | + hookID string, |
| 74 | +) (string, error) { |
| 75 | + resourceName := r.Names.Original |
| 76 | + if resourceName == "" || hookID == "" { |
| 77 | + return "", nil |
| 78 | + } |
| 79 | + c := r.Config() |
| 80 | + if c == nil { |
| 81 | + return "", nil |
| 82 | + } |
| 83 | + rConfig, ok := c.Resources[resourceName] |
| 84 | + if !ok { |
| 85 | + return "", nil |
| 86 | + } |
| 87 | + hook, ok := rConfig.Hooks[hookID] |
| 88 | + if !ok { |
| 89 | + return "", nil |
| 90 | + } |
| 91 | + if hook.Code != nil { |
| 92 | + return *hook.Code, nil |
| 93 | + } |
| 94 | + if hook.TemplatePath == nil { |
| 95 | + err := fmt.Errorf( |
| 96 | + "resource %s hook config for %s is invalid. Need either code or template_path", |
| 97 | + resourceName, hookID, |
| 98 | + ) |
| 99 | + return "", err |
| 100 | + } |
| 101 | + for _, basePath := range templateBasePaths { |
| 102 | + tplPath := filepath.Join(basePath, *hook.TemplatePath) |
| 103 | + if !ackutil.FileExists(tplPath) { |
| 104 | + continue |
| 105 | + } |
| 106 | + tplContents, err := ioutil.ReadFile(tplPath) |
| 107 | + if err != nil { |
| 108 | + err := fmt.Errorf( |
| 109 | + "resource %s hook config for %s is invalid: error reading %s: %s", |
| 110 | + resourceName, hookID, tplPath, err, |
| 111 | + ) |
| 112 | + return "", err |
| 113 | + } |
| 114 | + t := ttpl.New(tplPath) |
| 115 | + if t, err = t.Parse(string(tplContents)); err != nil { |
| 116 | + err := fmt.Errorf( |
| 117 | + "resource %s hook config for %s is invalid: error parsing %s: %s", |
| 118 | + resourceName, hookID, tplPath, err, |
| 119 | + ) |
| 120 | + return "", err |
| 121 | + } |
| 122 | + var b bytes.Buffer |
| 123 | + // TODO(jaypipes): Instead of nil for template vars here, maybe pass in |
| 124 | + // a struct of variables? |
| 125 | + if err := t.Execute(&b, nil); err != nil { |
| 126 | + err := fmt.Errorf( |
| 127 | + "resource %s hook config for %s is invalid: error executing %s: %s", |
| 128 | + resourceName, hookID, tplPath, err, |
| 129 | + ) |
| 130 | + return "", err |
| 131 | + } |
| 132 | + return b.String(), nil |
| 133 | + } |
| 134 | + err := fmt.Errorf( |
| 135 | + "resource %s hook config for %s is invalid: template_path %s not found", |
| 136 | + resourceName, hookID, *hook.TemplatePath, |
| 137 | + ) |
| 138 | + return "", err |
| 139 | +} |
0 commit comments