Skip to content

Commit

Permalink
Merge pull request crossplane-contrib#1917 from MisterMX/refactor/cli…
Browse files Browse the repository at this point in the history
…ent-to-utils
  • Loading branch information
MisterMX authored Oct 18, 2023
2 parents 04154fc + 5a99273 commit dd7f4a0
Show file tree
Hide file tree
Showing 469 changed files with 6,169 additions and 5,796 deletions.
10 changes: 5 additions & 5 deletions CODE_GENERATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,22 @@ func SetupStage(mgr ctrl.Manager, o controller.Options) error {
}

func preObserve(_ context.Context, cr *svcapitypes.Stage, obj *svcsdk.DescribeStageInput) error {
obj.StageName = aws.String(meta.GetExternalName(cr))
obj.StageName = pointer.String(meta.GetExternalName(cr))
return nil
}

func preCreate(_ context.Context, cr *svcapitypes.Stage, obj *svcsdk.CreateStageInput) error {
obj.StageName = aws.String(meta.GetExternalName(cr))
obj.StageName = pointer.String(meta.GetExternalName(cr))
return nil
}

func preUpdate(_ context.Context, cr *svcapitypes.Stage, obj *svcsdk.UpdateStageInput) error {
obj.StageName = aws.String(meta.GetExternalName(cr))
obj.StageName = pointer.String(meta.GetExternalName(cr))
return nil
}

func preDelete(_ context.Context, cr *svcapitypes.Stage, obj *svcsdk.DeleteStageInput) error {
obj.StageName = aws.String(meta.GetExternalName(cr))
obj.StageName = pointer.String(meta.GetExternalName(cr))
return nil
}
```
Expand Down Expand Up @@ -344,7 +344,7 @@ func postObserve(_ context.Context, cr *svcapitypes.Backup, resp *svcsdk.Describ
if err != nil {
return managed.ExternalObservation{}, err
}
switch aws.StringValue(resp.BackupDescription.BackupDetails.BackupStatus) {
switch pointer.StringValue(resp.BackupDescription.BackupDetails.BackupStatus) {
case string(svcapitypes.BackupStatus_SDK_AVAILABLE):
cr.SetConditions(xpv1.Available())
case string(svcapitypes.BackupStatus_SDK_CREATING):
Expand Down
108 changes: 0 additions & 108 deletions apis/ec2/manualv1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@ limitations under the License.
package manualv1alpha1

import (
"sort"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

awsclients "github.com/crossplane-contrib/provider-aws/pkg/clients"
)

// BlockDeviceMapping describes a block device mapping.
Expand Down Expand Up @@ -874,105 +868,3 @@ type TagSpecification struct {
// The tags to apply to the resource
Tags []Tag `json:"tags"`
}

// BuildFromEC2Tags returns a list of tags, off of the given ec2 tags
func BuildFromEC2Tags(tags []types.Tag) []Tag {
if len(tags) < 1 {
return nil
}
res := make([]Tag, len(tags))
for i, t := range tags {
res[i] = Tag{awsclients.StringValue(t.Key), awsclients.StringValue(t.Value)}
}

return res
}

// GenerateEC2Tags generates a tag array with type that EC2 client expects.
func GenerateEC2Tags(tags []Tag) []types.Tag {
res := make([]types.Tag, len(tags))
for i, t := range tags {
res[i] = types.Tag{Key: aws.String(t.Key), Value: aws.String(t.Value)}
}
return res
}

// CompareGroupNames compares slices of group names and ec2.GroupIdentifier
func CompareGroupNames(groupNames []string, ec2Groups []types.GroupIdentifier) bool {
if len(groupNames) != len(ec2Groups) {
return false
}

sortGroupNames(groupNames, ec2Groups)

for i, g := range groupNames {
if g != *ec2Groups[i].GroupName {
return false
}
}

return true
}

// CompareGroupIDs compares slices of group IDs and ec2.GroupIdentifier
func CompareGroupIDs(groupIDs []string, ec2Groups []types.GroupIdentifier) bool {
if len(groupIDs) != len(ec2Groups) {
return false
}

sortGroupIDs(groupIDs, ec2Groups)

for i, g := range groupIDs {
if g != *ec2Groups[i].GroupId {
return false
}
}

return true
}

// sortGroupNames sorts slice of string and ec2.GroupIdentifier on 'GroupName'
func sortGroupNames(groupNames []string, ec2Groups []types.GroupIdentifier) {
sort.Strings(groupNames)

sort.Slice(ec2Groups, func(i, j int) bool {
return *ec2Groups[i].GroupName < *ec2Groups[j].GroupName
})
}

// sortGroupNames sorts slice of string and ec2.GroupIdentifier on 'GroupName'
func sortGroupIDs(groupIDs []string, ec2Groups []types.GroupIdentifier) {
sort.Strings(groupIDs)

sort.Slice(ec2Groups, func(i, j int) bool {
return *ec2Groups[i].GroupId < *ec2Groups[j].GroupId
})
}

// CompareTags compares arrays of manualv1alpha1.Tag and ec2.Tag
func CompareTags(tags []Tag, ec2Tags []types.Tag) bool {
if len(tags) != len(ec2Tags) {
return false
}

SortTags(tags, ec2Tags)

for i, t := range tags {
if t.Key != *ec2Tags[i].Key || t.Value != *ec2Tags[i].Value {
return false
}
}

return true
}

// SortTags sorts array of v1beta1.Tag and ec2.Tag on 'Key'
func SortTags(tags []Tag, ec2Tags []types.Tag) {
sort.Slice(tags, func(i, j int) bool {
return tags[i].Key < tags[j].Key
})

sort.Slice(ec2Tags, func(i, j int) bool {
return *ec2Tags[i].Key < *ec2Tags[j].Key
})
}
57 changes: 0 additions & 57 deletions apis/ec2/v1beta1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ limitations under the License.

package v1beta1

import (
"sort"

"github.com/aws/aws-sdk-go-v2/aws"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
)

// Tag defines a tag
type Tag struct {

Expand All @@ -32,53 +25,3 @@ type Tag struct {
// Value is the value of the tag.
Value string `json:"value"`
}

// BuildFromEC2Tags returns a list of tags, off of the given ec2 tags
func BuildFromEC2Tags(tags []ec2types.Tag) []Tag {
if len(tags) < 1 {
return nil
}
res := make([]Tag, len(tags))
for i, t := range tags {
res[i] = Tag{aws.ToString(t.Key), aws.ToString(t.Value)}
}

return res
}

// GenerateEC2Tags generates a tag array with type that EC2 client expects.
func GenerateEC2Tags(tags []Tag) []ec2types.Tag {
res := make([]ec2types.Tag, len(tags))
for i, t := range tags {
res[i] = ec2types.Tag{Key: aws.String(t.Key), Value: aws.String(t.Value)}
}
return res
}

// CompareTags compares arrays of v1beta1.Tag and ec2type.Tag
func CompareTags(tags []Tag, ec2Tags []ec2types.Tag) bool {
if len(tags) != len(ec2Tags) {
return false
}

SortTags(tags, ec2Tags)

for i, t := range tags {
if t.Key != *ec2Tags[i].Key || t.Value != *ec2Tags[i].Value {
return false
}
}

return true
}

// SortTags sorts array of v1beta1.Tag and ec2type.Tag on 'Key'
func SortTags(tags []Tag, ec2Tags []ec2types.Tag) {
sort.Slice(tags, func(i, j int) bool {
return tags[i].Key < tags[j].Key
})

sort.Slice(ec2Tags, func(i, j int) bool {
return *ec2Tags[i].Key < *ec2Tags[j].Key
})
}
4 changes: 2 additions & 2 deletions apis/ecs/v1alpha1/referencers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package v1alpha1

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/crossplane/crossplane-runtime/pkg/reference"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"k8s.io/utils/ptr"

elbv2 "github.com/crossplane-contrib/provider-aws/apis/elbv2/v1alpha1"
)
Expand All @@ -15,6 +15,6 @@ func LoadBalancerName() reference.ExtractValueFn {
if !ok {
return ""
}
return aws.StringValue(lb.Status.AtProvider.LoadBalancerName)
return ptr.Deref(lb.Status.AtProvider.LoadBalancerName, "")
}
}
9 changes: 4 additions & 5 deletions apis/servicediscovery/v1alpha1/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package v1alpha1
import (
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/meta"

aws "github.com/crossplane-contrib/provider-aws/pkg/clients"
"k8s.io/utils/ptr"
)

// AnnotationKeyOperationID is the key in the annotations map of a
Expand Down Expand Up @@ -61,7 +60,7 @@ func (in *HTTPNamespace) GetOperationID() *string {

// SetOperationID sets the last operation id.
func (in *HTTPNamespace) SetOperationID(id *string) {
meta.AddAnnotations(in, map[string]string{AnnotationKeyOperationID: aws.StringValue(id)})
meta.AddAnnotations(in, map[string]string{AnnotationKeyOperationID: ptr.Deref(id, "")})
}

// GetDescription returns the description.
Expand Down Expand Up @@ -89,7 +88,7 @@ func (in *PrivateDNSNamespace) GetOperationID() *string {

// SetOperationID sets the last operation id.
func (in *PrivateDNSNamespace) SetOperationID(id *string) {
meta.AddAnnotations(in, map[string]string{AnnotationKeyOperationID: aws.StringValue(id)})
meta.AddAnnotations(in, map[string]string{AnnotationKeyOperationID: ptr.Deref(id, "")})
}

// GetDescription returns the description.
Expand Down Expand Up @@ -120,7 +119,7 @@ func (in *PublicDNSNamespace) GetOperationID() *string {

// SetOperationID sets the last operation id.
func (in *PublicDNSNamespace) SetOperationID(id *string) {
meta.AddAnnotations(in, map[string]string{AnnotationKeyOperationID: aws.StringValue(id)})
meta.AddAnnotations(in, map[string]string{AnnotationKeyOperationID: ptr.Deref(id, "")})
}

// GetDescription returns the description.
Expand Down
8 changes: 4 additions & 4 deletions pkg/clients/acm/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
acmtypes "github.com/aws/aws-sdk-go-v2/service/acm/types"

"github.com/crossplane-contrib/provider-aws/apis/acm/v1beta1"
awsclients "github.com/crossplane-contrib/provider-aws/pkg/clients"
"github.com/crossplane-contrib/provider-aws/pkg/utils/pointer"
)

// Client defines the CertificateManager operations
Expand Down Expand Up @@ -100,7 +100,7 @@ func GenerateCertificateStatus(certificate types.CertificateDetail) v1beta1.Cert
// LateInitializeCertificate fills the empty fields in *v1beta1.CertificateParameters with
// the values seen in iam.Certificate.
func LateInitializeCertificate(in *v1beta1.CertificateParameters, certificate *types.CertificateDetail) {
in.CertificateAuthorityARN = awsclients.LateInitializeStringPtr(in.CertificateAuthorityARN, certificate.CertificateAuthorityArn)
in.CertificateAuthorityARN = pointer.LateInitializeStringPtr(in.CertificateAuthorityARN, certificate.CertificateAuthorityArn)
if in.Options == nil && certificate.Options != nil {
in.Options = &v1beta1.CertificateOptions{
CertificateTransparencyLoggingPreference: string(certificate.Options.CertificateTransparencyLoggingPreference),
Expand All @@ -118,8 +118,8 @@ func LateInitializeCertificate(in *v1beta1.CertificateParameters, certificate *t
in.DomainValidationOptions = make([]*v1beta1.DomainValidationOption, len(certificate.DomainValidationOptions))
for i, val := range certificate.DomainValidationOptions {
in.DomainValidationOptions[i] = &v1beta1.DomainValidationOption{
DomainName: awsclients.StringValue(val.DomainName),
ValidationDomain: awsclients.StringValue(val.ValidationDomain),
DomainName: pointer.StringValue(val.DomainName),
ValidationDomain: pointer.StringValue(val.ValidationDomain),
}
}
}
Expand Down
Loading

0 comments on commit dd7f4a0

Please sign in to comment.