- 
                Notifications
    You must be signed in to change notification settings 
- Fork 226
Add additional keys as identifiers #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -3,9 +3,14 @@ | |
| package {{ .CRD.Names.Snake }} | ||
|  | ||
| import ( | ||
| "reflect" | ||
| "strings" | ||
|  | ||
| ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" | ||
| acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" | ||
| {{- if $idField := .CRD.SpecIdentifierField }} | ||
| ackerrors "github.com/aws-controllers-k8s/runtime/pkg/errors" | ||
| {{- end }} | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| k8srt "k8s.io/apimachinery/pkg/runtime" | ||
|  | ||
|  | @@ -67,15 +72,38 @@ func (r *resource) SetObjectMeta(meta metav1.ObjectMeta) { | |
| } | ||
|  | ||
| // SetIdentifiers sets the Spec or Status field that is referenced as the unique | ||
| // resource identifier | ||
| // resource identifier and any additional spec fields that may be required for | ||
| // describing the resource. | ||
| func (r *resource) SetIdentifiers(identifier *ackv1alpha1.AWSIdentifiers) error { | ||
| {{- if $idField := .CRD.SpecIdentifierField }} | ||
| if identifier.NameOrID == nil { | ||
| return ackerrors.MissingNameIdentifier | ||
| } | ||
| r.ko.Spec.{{ $idField }} = identifier.NameOrID | ||
| {{- else }} | ||
| if r.ko.Status.ACKResourceMetadata == nil { | ||
| r.ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{} | ||
| } | ||
| r.ko.Status.ACKResourceMetadata.ARN = identifier.ARN | ||
| {{- end }} | ||
|  | ||
| if len(identifier.AdditionalKeys) == 0 { | ||
| return nil | ||
| } | ||
|  | ||
| specRef := reflect.Indirect(reflect.ValueOf(&r.ko.Spec)) | ||
| specType := specRef.Type() | ||
|  | ||
| // Iterate over spec fields and associate corresponding json tags | ||
| for i := 0; i < specRef.NumField(); i++ { | ||
| // Get only the first field in the json tag (the field name) | ||
| jsonTag := strings.Split(specType.Field(i).Tag.Get("json"), ",")[0] | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm OK with using the  In the future, we may want to take an approach similar to how we handle TerminalConditions and simply generate a  | ||
| val, ok := identifier.AdditionalKeys[jsonTag] | ||
| if ok { | ||
| // Set the corresponding field with the value from the mapping | ||
| specRef.FieldByName(specType.Field(i).Name).Set(reflect.ValueOf(val)) | ||
| } | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if the jsonTag isn't found in the  | ||
| } | ||
|  | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can remove this now that #61 is merged.