diff --git a/pkg/composite/gen.go b/pkg/composite/gen.go index ade921006c..6563e58813 100644 --- a/pkg/composite/gen.go +++ b/pkg/composite/gen.go @@ -1661,6 +1661,150 @@ type MutualTls struct { NullFields []string `json:"-"` } +// NetworkEndpoint is a composite type wrapping the Alpha, Beta, and GA methods for its GCE equivalent +type NetworkEndpoint struct { + // Metadata defined as annotations on the network endpoint. + Annotations map[string]string `json:"annotations,omitempty"` + // Optional fully qualified domain name of network endpoint. This can + // only be specified when NetworkEndpointGroup.network_endpoint_type is + // NON_GCP_FQDN_PORT. + Fqdn string `json:"fqdn,omitempty"` + // The name for a specific VM instance that the IP address belongs to. + // This is required for network endpoints of type GCE_VM_IP_PORT. The + // instance must be in the same zone of network endpoint group. + // + // The name must be 1-63 characters long, and comply with RFC1035. + Instance string `json:"instance,omitempty"` + // Optional IPv4 address of network endpoint. The IP address must belong + // to a VM in GCE (either the primary IP or as part of an aliased IP + // range). If the IP address is not specified, then the primary IP + // address for the VM instance in the network that the network endpoint + // group belongs to will be used. + IpAddress string `json:"ipAddress,omitempty"` + // Optional port number of network endpoint. If not specified and the + // NetworkEndpointGroup.network_endpoint_type is GCE_IP_PORT, the + // defaultPort for the network endpoint group will be used. + Port int64 `json:"port,omitempty"` + ForceSendFields []string `json:"-"` + NullFields []string `json:"-"` +} + +// NetworkEndpointGroup is a composite type wrapping the Alpha, Beta, and GA methods for its GCE equivalent +type NetworkEndpointGroup struct { + // Version keeps track of the intended compute version for this NetworkEndpointGroup. + // Note that the compute API's do not contain this field. It is for our + // own bookkeeping purposes. + Version meta.Version `json:"-"` + // Scope keeps track of the intended type of the service (e.g. Global) + // This is also an internal field purely for bookkeeping purposes + Scope meta.KeyType `json:"-"` + + // Metadata defined as annotations on the network endpoint group. + Annotations map[string]string `json:"annotations,omitempty"` + // [Output Only] Creation timestamp in RFC3339 text format. + CreationTimestamp string `json:"creationTimestamp,omitempty"` + // The default port used if the port number is not specified in the + // network endpoint. + DefaultPort int64 `json:"defaultPort,omitempty"` + // An optional description of this resource. Provide this property when + // you create the resource. + Description string `json:"description,omitempty"` + // [Output Only] The unique identifier for the resource. This identifier + // is defined by the server. + Id uint64 `json:"id,omitempty,string"` + // [Output Only] Type of the resource. Always + // compute#networkEndpointGroup for network endpoint group. + Kind string `json:"kind,omitempty"` + // This field is only valid when the network endpoint group is used for + // load balancing. [Deprecated] This field is deprecated. + LoadBalancer *NetworkEndpointGroupLbNetworkEndpointGroup `json:"loadBalancer,omitempty"` + // Name of the resource; provided by the client when the resource is + // created. The name must be 1-63 characters long, and comply with + // RFC1035. Specifically, the name must be 1-63 characters long and + // match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` which means + // the first character must be a lowercase letter, and all following + // characters must be a dash, lowercase letter, or digit, except the + // last character, which cannot be a dash. + Name string `json:"name,omitempty"` + // The URL of the network to which all network endpoints in the NEG + // belong. Uses "default" project network if unspecified. + Network string `json:"network,omitempty"` + // Type of network endpoints in this network endpoint group. Currently + // the only supported value is GCE_VM_IP_PORT. + NetworkEndpointType string `json:"networkEndpointType,omitempty"` + // [Output Only] Server-defined URL for the resource. + SelfLink string `json:"selfLink,omitempty"` + // [Output Only] Server-defined URL for this resource with the resource + // id. + SelfLinkWithId string `json:"selfLinkWithId,omitempty"` + // [Output only] Number of network endpoints in the network endpoint + // group. + Size int64 `json:"size,omitempty"` + // Optional URL of the subnetwork to which all network endpoints in the + // NEG belong. + Subnetwork string `json:"subnetwork,omitempty"` + // Specify the type of this network endpoint group. Only LOAD_BALANCING + // is valid for now. + Type string `json:"type,omitempty"` + // [Output Only] The URL of the zone where the network endpoint group is + // located. + Zone string `json:"zone,omitempty"` + googleapi.ServerResponse `json:"-"` + ForceSendFields []string `json:"-"` + NullFields []string `json:"-"` +} + +// NetworkEndpointGroupLbNetworkEndpointGroup is a composite type wrapping the Alpha, Beta, and GA methods for its GCE equivalent +type NetworkEndpointGroupLbNetworkEndpointGroup struct { + // The default port used if the port number is not specified in the + // network endpoint. [Deprecated] This field is deprecated. + DefaultPort int64 `json:"defaultPort,omitempty"` + // The URL of the network to which all network endpoints in the NEG + // belong. Uses "default" project network if unspecified. [Deprecated] + // This field is deprecated. + Network string `json:"network,omitempty"` + // Optional URL of the subnetwork to which all network endpoints in the + // NEG belong. [Deprecated] This field is deprecated. + Subnetwork string `json:"subnetwork,omitempty"` + // [Output Only] The URL of the zone where the network endpoint group is + // located. [Deprecated] This field is deprecated. + Zone string `json:"zone,omitempty"` + ForceSendFields []string `json:"-"` + NullFields []string `json:"-"` +} + +// NetworkEndpointGroupsAttachEndpointsRequest is a composite type wrapping the Alpha, Beta, and GA methods for its GCE equivalent +type NetworkEndpointGroupsAttachEndpointsRequest struct { + // Version keeps track of the intended compute version for this NetworkEndpointGroupsAttachEndpointsRequest. + // Note that the compute API's do not contain this field. It is for our + // own bookkeeping purposes. + Version meta.Version `json:"-"` + // Scope keeps track of the intended type of the service (e.g. Global) + // This is also an internal field purely for bookkeeping purposes + Scope meta.KeyType `json:"-"` + + // The list of network endpoints to be attached. + NetworkEndpoints []*NetworkEndpoint `json:"networkEndpoints,omitempty"` + ForceSendFields []string `json:"-"` + NullFields []string `json:"-"` +} + +// NetworkEndpointGroupsDetachEndpointsRequest is a composite type wrapping the Alpha, Beta, and GA methods for its GCE equivalent +type NetworkEndpointGroupsDetachEndpointsRequest struct { + // Version keeps track of the intended compute version for this NetworkEndpointGroupsDetachEndpointsRequest. + // Note that the compute API's do not contain this field. It is for our + // own bookkeeping purposes. + Version meta.Version `json:"-"` + // Scope keeps track of the intended type of the service (e.g. Global) + // This is also an internal field purely for bookkeeping purposes + Scope meta.KeyType `json:"-"` + + // The list of network endpoints to be detached. + NetworkEndpoints []*NetworkEndpoint `json:"networkEndpoints,omitempty"` + ForceSendFields []string `json:"-"` + NullFields []string `json:"-"` +} + // OriginAuthenticationMethod is a composite type wrapping the Alpha, Beta, and GA methods for its GCE equivalent type OriginAuthenticationMethod struct { Jwt *Jwt `json:"jwt,omitempty"` @@ -2639,7 +2783,6 @@ func GetBackendService(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) if err != nil { return nil, err } - compositeType.Version = version return compositeType, nil } @@ -2695,7 +2838,7 @@ func ToBackendServiceList(objs interface{}) ([]*BackendService, error) { err := copyViaJSON(&result, objs) if err != nil { - return nil, fmt.Errorf("could not copy object %v to list of BackendService via JSON: %v", objs, err) + return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err) } return result, nil } @@ -2706,7 +2849,7 @@ func ToBackendService(obj interface{}) (*BackendService, error) { backendService := &BackendService{} err := copyViaJSON(backendService, obj) if err != nil { - return nil, fmt.Errorf("could not copy object %+v to BackendService via JSON: %v", obj, err) + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, backendService, err) } return backendService, nil @@ -2718,7 +2861,7 @@ func (backendService *BackendService) ToAlpha() (*computealpha.BackendService, e alpha := &computealpha.BackendService{} err := copyViaJSON(alpha, backendService) if err != nil { - return nil, fmt.Errorf("error converting BackendService to compute alpha type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", backendService, err) } // Set force send fields. This is a temporary hack. if alpha.CdnPolicy != nil && alpha.CdnPolicy.CacheKeyPolicy != nil { @@ -2737,7 +2880,7 @@ func (backendService *BackendService) ToBeta() (*computebeta.BackendService, err beta := &computebeta.BackendService{} err := copyViaJSON(beta, backendService) if err != nil { - return nil, fmt.Errorf("error converting BackendService to compute beta type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", backendService, err) } // Set force send fields. This is a temporary hack. if beta.CdnPolicy != nil && beta.CdnPolicy.CacheKeyPolicy != nil { @@ -2756,7 +2899,7 @@ func (backendService *BackendService) ToGA() (*compute.BackendService, error) { ga := &compute.BackendService{} err := copyViaJSON(ga, backendService) if err != nil { - return nil, fmt.Errorf("error converting BackendService to compute ga type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", backendService, err) } // Set force send fields. This is a temporary hack. if ga.CdnPolicy != nil && ga.CdnPolicy.CacheKeyPolicy != nil { @@ -2797,6 +2940,7 @@ func CreateForwardingRule(gceCloud *gce.Cloud, key *meta.Key, forwardingRule *Fo switch key.Type() { case meta.Regional: klog.V(3).Infof("Creating beta region ForwardingRule %v", beta.Name) + beta.Region = key.Region return mc.Observe(gceCloud.Compute().BetaForwardingRules().Insert(ctx, key, beta)) default: klog.V(3).Infof("Creating beta ForwardingRule %v", beta.Name) @@ -2810,6 +2954,7 @@ func CreateForwardingRule(gceCloud *gce.Cloud, key *meta.Key, forwardingRule *Fo switch key.Type() { case meta.Regional: klog.V(3).Infof("Creating ga region ForwardingRule %v", ga.Name) + ga.Region = key.Region return mc.Observe(gceCloud.Compute().ForwardingRules().Insert(ctx, key, ga)) default: klog.V(3).Infof("Creating ga ForwardingRule %v", ga.Name) @@ -2818,6 +2963,42 @@ func CreateForwardingRule(gceCloud *gce.Cloud, key *meta.Key, forwardingRule *Fo } } +func DeleteForwardingRule(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) error { + ctx, cancel := cloudprovider.ContextWithCallTimeout() + defer cancel() + mc := compositemetrics.NewMetricContext("ForwardingRule", "delete", key.Region, key.Zone, string(version)) + + switch version { + case meta.VersionAlpha: + switch key.Type() { + case meta.Regional: + klog.V(3).Infof("Deleting alpha region ForwardingRule %v", key.Name) + return mc.Observe(gceCloud.Compute().AlphaForwardingRules().Delete(ctx, key)) + default: + klog.V(3).Infof("Deleting alpha ForwardingRule %v", key.Name) + return mc.Observe(gceCloud.Compute().AlphaGlobalForwardingRules().Delete(ctx, key)) + } + case meta.VersionBeta: + switch key.Type() { + case meta.Regional: + klog.V(3).Infof("Deleting beta region ForwardingRule %v", key.Name) + return mc.Observe(gceCloud.Compute().BetaForwardingRules().Delete(ctx, key)) + default: + klog.V(3).Infof("Deleting beta ForwardingRule %v", key.Name) + return mc.Observe(gceCloud.Compute().BetaGlobalForwardingRules().Delete(ctx, key)) + } + default: + switch key.Type() { + case meta.Regional: + klog.V(3).Infof("Deleting ga region ForwardingRule %v", key.Name) + return mc.Observe(gceCloud.Compute().ForwardingRules().Delete(ctx, key)) + default: + klog.V(3).Infof("Deleting ga ForwardingRule %v", key.Name) + return mc.Observe(gceCloud.Compute().GlobalForwardingRules().Delete(ctx, key)) + } + } +} + func GetForwardingRule(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) (*ForwardingRule, error) { ctx, cancel := cloudprovider.ContextWithCallTimeout() defer cancel() @@ -2861,11 +3042,9 @@ func GetForwardingRule(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) if err != nil { return nil, err } - if key.Type() == meta.Regional { compositeType.Scope = meta.Regional } - compositeType.Version = version return compositeType, nil } @@ -2909,6 +3088,7 @@ func ListForwardingRules(gceCloud *gce.Cloud, key *meta.Key, version meta.Versio if err != nil { return nil, mc.Observe(err) } + compositeObjs, err := ToForwardingRuleList(gceObjs) if err != nil { return nil, err @@ -2919,42 +3099,6 @@ func ListForwardingRules(gceCloud *gce.Cloud, key *meta.Key, version meta.Versio return compositeObjs, nil } -func DeleteForwardingRule(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) error { - ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("ForwardingRule", "delete", key.Region, key.Zone, string(version)) - - switch version { - case meta.VersionAlpha: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Deleting alpha region ForwardingRule %v", key.Name) - return mc.Observe(gceCloud.Compute().AlphaForwardingRules().Delete(ctx, key)) - default: - klog.V(3).Infof("Deleting alpha ForwardingRule %v", key.Name) - return mc.Observe(gceCloud.Compute().AlphaGlobalForwardingRules().Delete(ctx, key)) - } - case meta.VersionBeta: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Deleting beta region ForwardingRule %v", key.Name) - return mc.Observe(gceCloud.Compute().BetaForwardingRules().Delete(ctx, key)) - default: - klog.V(3).Infof("Deleting beta ForwardingRule %v", key.Name) - return mc.Observe(gceCloud.Compute().BetaGlobalForwardingRules().Delete(ctx, key)) - } - default: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Deleting ga region ForwardingRule %v", key.Name) - return mc.Observe(gceCloud.Compute().ForwardingRules().Delete(ctx, key)) - default: - klog.V(3).Infof("Deleting ga ForwardingRule %v", key.Name) - return mc.Observe(gceCloud.Compute().GlobalForwardingRules().Delete(ctx, key)) - } - } -} - // ToForwardingRuleList converts a list of compute alpha, beta or GA // ForwardingRule into a list of our composite type. func ToForwardingRuleList(objs interface{}) ([]*ForwardingRule, error) { @@ -2962,7 +3106,7 @@ func ToForwardingRuleList(objs interface{}) ([]*ForwardingRule, error) { err := copyViaJSON(&result, objs) if err != nil { - return nil, fmt.Errorf("Could not copy object %v to list of ForwardingRule via JSON: %v", objs, err) + return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err) } return result, nil } @@ -2973,7 +3117,7 @@ func ToForwardingRule(obj interface{}) (*ForwardingRule, error) { forwardingRule := &ForwardingRule{} err := copyViaJSON(forwardingRule, obj) if err != nil { - return nil, fmt.Errorf("could not copy object %+v to ForwardingRule via JSON: %v", obj, err) + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, forwardingRule, err) } return forwardingRule, nil @@ -2985,7 +3129,7 @@ func (forwardingRule *ForwardingRule) ToAlpha() (*computealpha.ForwardingRule, e alpha := &computealpha.ForwardingRule{} err := copyViaJSON(alpha, forwardingRule) if err != nil { - return nil, fmt.Errorf("error converting ForwardingRule to compute alpha type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", forwardingRule, err) } return alpha, nil @@ -2997,7 +3141,7 @@ func (forwardingRule *ForwardingRule) ToBeta() (*computebeta.ForwardingRule, err beta := &computebeta.ForwardingRule{} err := copyViaJSON(beta, forwardingRule) if err != nil { - return nil, fmt.Errorf("error converting ForwardingRule to compute beta type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", forwardingRule, err) } return beta, nil @@ -3009,7 +3153,7 @@ func (forwardingRule *ForwardingRule) ToGA() (*compute.ForwardingRule, error) { ga := &compute.ForwardingRule{} err := copyViaJSON(ga, forwardingRule) if err != nil { - return nil, fmt.Errorf("error converting ForwardingRule to compute ga type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", forwardingRule, err) } return ga, nil @@ -3167,7 +3311,6 @@ func GetHealthCheck(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) (* if err != nil { return nil, err } - compositeType.Version = version return compositeType, nil } @@ -3223,7 +3366,7 @@ func ToHealthCheckList(objs interface{}) ([]*HealthCheck, error) { err := copyViaJSON(&result, objs) if err != nil { - return nil, fmt.Errorf("could not copy object %v to list of HealthCheck via JSON: %v", objs, err) + return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err) } return result, nil } @@ -3234,7 +3377,7 @@ func ToHealthCheck(obj interface{}) (*HealthCheck, error) { healthCheck := &HealthCheck{} err := copyViaJSON(healthCheck, obj) if err != nil { - return nil, fmt.Errorf("could not copy object %+v to HealthCheck via JSON: %v", obj, err) + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, healthCheck, err) } return healthCheck, nil @@ -3246,7 +3389,7 @@ func (healthCheck *HealthCheck) ToAlpha() (*computealpha.HealthCheck, error) { alpha := &computealpha.HealthCheck{} err := copyViaJSON(alpha, healthCheck) if err != nil { - return nil, fmt.Errorf("error converting HealthCheck to compute alpha type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", healthCheck, err) } return alpha, nil @@ -3258,7 +3401,7 @@ func (healthCheck *HealthCheck) ToBeta() (*computebeta.HealthCheck, error) { beta := &computebeta.HealthCheck{} err := copyViaJSON(beta, healthCheck) if err != nil { - return nil, fmt.Errorf("error converting HealthCheck to compute beta type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", healthCheck, err) } return beta, nil @@ -3270,7 +3413,366 @@ func (healthCheck *HealthCheck) ToGA() (*compute.HealthCheck, error) { ga := &compute.HealthCheck{} err := copyViaJSON(ga, healthCheck) if err != nil { - return nil, fmt.Errorf("error converting HealthCheck to compute ga type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", healthCheck, err) + } + + return ga, nil +} + +func CreateNetworkEndpointGroup(gceCloud *gce.Cloud, key *meta.Key, networkEndpointGroup *NetworkEndpointGroup) error { + ctx, cancel := cloudprovider.ContextWithCallTimeout() + defer cancel() + mc := compositemetrics.NewMetricContext("NetworkEndpointGroup", "create", key.Region, key.Zone, string(networkEndpointGroup.Version)) + switch key.Type() { + case meta.Zonal: + default: + return fmt.Errorf("Key %v not valid for zonal resource NetworkEndpointGroup %v", key, key.Name) + } + + switch networkEndpointGroup.Version { + case meta.VersionAlpha: + alpha, err := networkEndpointGroup.ToAlpha() + if err != nil { + return err + } + klog.V(3).Infof("Creating alpha zonal NetworkEndpointGroup %v", alpha.Name) + return mc.Observe(gceCloud.Compute().AlphaNetworkEndpointGroups().Insert(ctx, key, alpha)) + case meta.VersionBeta: + beta, err := networkEndpointGroup.ToBeta() + if err != nil { + return err + } + klog.V(3).Infof("Creating beta zonal NetworkEndpointGroup %v", beta.Name) + return mc.Observe(gceCloud.Compute().BetaNetworkEndpointGroups().Insert(ctx, key, beta)) + default: + ga, err := networkEndpointGroup.ToGA() + if err != nil { + return err + } + klog.V(3).Infof("Creating ga zonal NetworkEndpointGroup %v", ga.Name) + return mc.Observe(gceCloud.Compute().NetworkEndpointGroups().Insert(ctx, key, ga)) + } +} + +func DeleteNetworkEndpointGroup(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) error { + ctx, cancel := cloudprovider.ContextWithCallTimeout() + defer cancel() + mc := compositemetrics.NewMetricContext("NetworkEndpointGroup", "delete", key.Region, key.Zone, string(version)) + switch key.Type() { + case meta.Zonal: + default: + return fmt.Errorf("Key %v not valid for zonal resource NetworkEndpointGroup %v", key, key.Name) + } + + switch version { + case meta.VersionAlpha: + klog.V(3).Infof("Deleting alpha zonal NetworkEndpointGroup %v", key.Name) + return mc.Observe(gceCloud.Compute().AlphaNetworkEndpointGroups().Delete(ctx, key)) + case meta.VersionBeta: + klog.V(3).Infof("Deleting beta zonal NetworkEndpointGroup %v", key.Name) + return mc.Observe(gceCloud.Compute().BetaNetworkEndpointGroups().Delete(ctx, key)) + default: + klog.V(3).Infof("Deleting ga zonal NetworkEndpointGroup %v", key.Name) + return mc.Observe(gceCloud.Compute().NetworkEndpointGroups().Delete(ctx, key)) + } +} + +func GetNetworkEndpointGroup(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) (*NetworkEndpointGroup, error) { + ctx, cancel := cloudprovider.ContextWithCallTimeout() + defer cancel() + mc := compositemetrics.NewMetricContext("NetworkEndpointGroup", "get", key.Region, key.Zone, string(version)) + + var gceObj interface{} + var err error + switch key.Type() { + case meta.Zonal: + default: + return nil, fmt.Errorf("Key %v not valid for zonal resource NetworkEndpointGroup %v", key, key.Name) + } + switch version { + case meta.VersionAlpha: + klog.V(3).Infof("Getting alpha zonal NetworkEndpointGroup %v", key.Name) + gceObj, err = gceCloud.Compute().AlphaNetworkEndpointGroups().Get(ctx, key) + case meta.VersionBeta: + klog.V(3).Infof("Getting beta zonal NetworkEndpointGroup %v", key.Name) + gceObj, err = gceCloud.Compute().BetaNetworkEndpointGroups().Get(ctx, key) + + default: + klog.V(3).Infof("Getting ga zonal NetworkEndpointGroup %v", key.Name) + gceObj, err = gceCloud.Compute().NetworkEndpointGroups().Get(ctx, key) + } + if err != nil { + return nil, mc.Observe(err) + } + compositeType, err := ToNetworkEndpointGroup(gceObj) + if err != nil { + return nil, err + } + compositeType.Scope = meta.Zonal + compositeType.Version = version + return compositeType, nil +} + +func ListNetworkEndpointGroups(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) ([]*NetworkEndpointGroup, error) { + ctx, cancel := cloudprovider.ContextWithCallTimeout() + defer cancel() + mc := compositemetrics.NewMetricContext("NetworkEndpointGroup", "get", key.Region, key.Zone, string(version)) + + var gceObjs interface{} + var err error + switch key.Type() { + case meta.Zonal: + default: + return nil, fmt.Errorf("Key %v not valid for zonal resource NetworkEndpointGroup %v", key, key.Name) + } + switch version { + case meta.VersionAlpha: + klog.V(3).Infof("Listing alpha zoneNetworkEndpointGroup") + gceObjs, err = gceCloud.Compute().AlphaNetworkEndpointGroups().List(ctx, key.Zone, filter.None) + case meta.VersionBeta: + klog.V(3).Infof("Listing beta zoneNetworkEndpointGroup") + gceObjs, err = gceCloud.Compute().BetaNetworkEndpointGroups().List(ctx, key.Zone, filter.None) + default: + klog.V(3).Infof("Listing ga zoneNetworkEndpointGroup") + gceObjs, err = gceCloud.Compute().NetworkEndpointGroups().List(ctx, key.Zone, filter.None) + } + if err != nil { + return nil, mc.Observe(err) + } + + compositeObjs, err := ToNetworkEndpointGroupList(gceObjs) + if err != nil { + return nil, err + } + for _, obj := range compositeObjs { + obj.Version = version + } + return compositeObjs, nil +} + +func AttachNetworkEndpoints(gceCloud *gce.Cloud, key *meta.Key, version meta.Version, req *NetworkEndpointGroupsAttachEndpointsRequest) error { + ctx, cancel := cloudprovider.ContextWithCallTimeout() + defer cancel() + mc := compositemetrics.NewMetricContext("NetworkEndpointGroup", "attach", key.Region, key.Zone, string(version)) + + switch key.Type() { + case meta.Zonal: + default: + return fmt.Errorf("Key %v not valid for zonal resource NetworkEndpointGroup %v", key, key.Name) + } + + switch version { + case meta.VersionAlpha: + alphareq, err := req.ToAlpha() + if err != nil { + return err + } + klog.V(3).Infof("Attaching to alpha zonal NetworkEndpointGroup %v", key.Name) + return mc.Observe(gceCloud.Compute().AlphaNetworkEndpointGroups().AttachNetworkEndpoints(ctx, key, alphareq)) + case meta.VersionBeta: + betareq, err := req.ToBeta() + if err != nil { + return err + } + klog.V(3).Infof("Attaching to beta zonal NetworkEndpointGroup %v", key.Name) + return mc.Observe(gceCloud.Compute().BetaNetworkEndpointGroups().AttachNetworkEndpoints(ctx, key, betareq)) + default: + gareq, err := req.ToGA() + if err != nil { + return err + } + klog.V(3).Infof("Attaching to ga zonal NetworkEndpointGroup %v", key.Name) + return mc.Observe(gceCloud.Compute().NetworkEndpointGroups().AttachNetworkEndpoints(ctx, key, gareq)) + } +} + +func DetachNetworkEndpoints(gceCloud *gce.Cloud, key *meta.Key, version meta.Version, req *NetworkEndpointGroupsDetachEndpointsRequest) error { + ctx, cancel := cloudprovider.ContextWithCallTimeout() + defer cancel() + mc := compositemetrics.NewMetricContext("NetworkEndpointGroup", "attach", key.Region, key.Zone, string(version)) + + switch key.Type() { + case meta.Zonal: + default: + return fmt.Errorf("Key %v not valid for zonal resource NetworkEndpointGroup %v", key, key.Name) + } + + switch version { + case meta.VersionAlpha: + alphareq, err := req.ToAlpha() + if err != nil { + return err + } + klog.V(3).Infof("Detaching from alpha zonal NetworkEndpointGroup %v", key.Name) + return mc.Observe(gceCloud.Compute().AlphaNetworkEndpointGroups().DetachNetworkEndpoints(ctx, key, alphareq)) + case meta.VersionBeta: + betareq, err := req.ToBeta() + if err != nil { + return err + } + klog.V(3).Infof("Detaching from beta zonal NetworkEndpointGroup %v", key.Name) + return mc.Observe(gceCloud.Compute().BetaNetworkEndpointGroups().DetachNetworkEndpoints(ctx, key, betareq)) + default: + gareq, err := req.ToGA() + if err != nil { + return err + } + klog.V(3).Infof("Detaching from ga zonal NetworkEndpointGroup %v", key.Name) + return mc.Observe(gceCloud.Compute().NetworkEndpointGroups().DetachNetworkEndpoints(ctx, key, gareq)) + } +} + +// ToNetworkEndpointGroupList converts a list of compute alpha, beta or GA +// NetworkEndpointGroup into a list of our composite type. +func ToNetworkEndpointGroupList(objs interface{}) ([]*NetworkEndpointGroup, error) { + result := []*NetworkEndpointGroup{} + + err := copyViaJSON(&result, objs) + if err != nil { + return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err) + } + return result, nil +} + +// ToNetworkEndpointGroup converts a compute alpha, beta or GA +// NetworkEndpointGroup into our composite type. +func ToNetworkEndpointGroup(obj interface{}) (*NetworkEndpointGroup, error) { + networkEndpointGroup := &NetworkEndpointGroup{} + err := copyViaJSON(networkEndpointGroup, obj) + if err != nil { + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, networkEndpointGroup, err) + } + + return networkEndpointGroup, nil +} + +// ToAlpha converts our composite type into an alpha type. +// This alpha type can be used in GCE API calls. +func (networkEndpointGroup *NetworkEndpointGroup) ToAlpha() (*computealpha.NetworkEndpointGroup, error) { + alpha := &computealpha.NetworkEndpointGroup{} + err := copyViaJSON(alpha, networkEndpointGroup) + if err != nil { + return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", networkEndpointGroup, err) + } + + return alpha, nil +} + +// ToBeta converts our composite type into an beta type. +// This beta type can be used in GCE API calls. +func (networkEndpointGroup *NetworkEndpointGroup) ToBeta() (*computebeta.NetworkEndpointGroup, error) { + beta := &computebeta.NetworkEndpointGroup{} + err := copyViaJSON(beta, networkEndpointGroup) + if err != nil { + return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", networkEndpointGroup, err) + } + + return beta, nil +} + +// ToGA converts our composite type into an ga type. +// This ga type can be used in GCE API calls. +func (networkEndpointGroup *NetworkEndpointGroup) ToGA() (*compute.NetworkEndpointGroup, error) { + ga := &compute.NetworkEndpointGroup{} + err := copyViaJSON(ga, networkEndpointGroup) + if err != nil { + return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", networkEndpointGroup, err) + } + + return ga, nil +} + +// ToNetworkEndpointGroupsAttachEndpointsRequest converts a compute alpha, beta or GA +// NetworkEndpointGroupsAttachEndpointsRequest into our composite type. +func ToNetworkEndpointGroupsAttachEndpointsRequest(obj interface{}) (*NetworkEndpointGroupsAttachEndpointsRequest, error) { + networkEndpointGroupsAttachEndpointsRequest := &NetworkEndpointGroupsAttachEndpointsRequest{} + err := copyViaJSON(networkEndpointGroupsAttachEndpointsRequest, obj) + if err != nil { + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, networkEndpointGroupsAttachEndpointsRequest, err) + } + + return networkEndpointGroupsAttachEndpointsRequest, nil +} + +// ToAlpha converts our composite type into an alpha type. +// This alpha type can be used in GCE API calls. +func (networkEndpointGroupsAttachEndpointsRequest *NetworkEndpointGroupsAttachEndpointsRequest) ToAlpha() (*computealpha.NetworkEndpointGroupsAttachEndpointsRequest, error) { + alpha := &computealpha.NetworkEndpointGroupsAttachEndpointsRequest{} + err := copyViaJSON(alpha, networkEndpointGroupsAttachEndpointsRequest) + if err != nil { + return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", networkEndpointGroupsAttachEndpointsRequest, err) + } + + return alpha, nil +} + +// ToBeta converts our composite type into an beta type. +// This beta type can be used in GCE API calls. +func (networkEndpointGroupsAttachEndpointsRequest *NetworkEndpointGroupsAttachEndpointsRequest) ToBeta() (*computebeta.NetworkEndpointGroupsAttachEndpointsRequest, error) { + beta := &computebeta.NetworkEndpointGroupsAttachEndpointsRequest{} + err := copyViaJSON(beta, networkEndpointGroupsAttachEndpointsRequest) + if err != nil { + return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", networkEndpointGroupsAttachEndpointsRequest, err) + } + + return beta, nil +} + +// ToGA converts our composite type into an ga type. +// This ga type can be used in GCE API calls. +func (networkEndpointGroupsAttachEndpointsRequest *NetworkEndpointGroupsAttachEndpointsRequest) ToGA() (*compute.NetworkEndpointGroupsAttachEndpointsRequest, error) { + ga := &compute.NetworkEndpointGroupsAttachEndpointsRequest{} + err := copyViaJSON(ga, networkEndpointGroupsAttachEndpointsRequest) + if err != nil { + return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", networkEndpointGroupsAttachEndpointsRequest, err) + } + + return ga, nil +} + +// ToNetworkEndpointGroupsDetachEndpointsRequest converts a compute alpha, beta or GA +// NetworkEndpointGroupsDetachEndpointsRequest into our composite type. +func ToNetworkEndpointGroupsDetachEndpointsRequest(obj interface{}) (*NetworkEndpointGroupsDetachEndpointsRequest, error) { + networkEndpointGroupsDetachEndpointsRequest := &NetworkEndpointGroupsDetachEndpointsRequest{} + err := copyViaJSON(networkEndpointGroupsDetachEndpointsRequest, obj) + if err != nil { + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, networkEndpointGroupsDetachEndpointsRequest, err) + } + + return networkEndpointGroupsDetachEndpointsRequest, nil +} + +// ToAlpha converts our composite type into an alpha type. +// This alpha type can be used in GCE API calls. +func (networkEndpointGroupsDetachEndpointsRequest *NetworkEndpointGroupsDetachEndpointsRequest) ToAlpha() (*computealpha.NetworkEndpointGroupsDetachEndpointsRequest, error) { + alpha := &computealpha.NetworkEndpointGroupsDetachEndpointsRequest{} + err := copyViaJSON(alpha, networkEndpointGroupsDetachEndpointsRequest) + if err != nil { + return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", networkEndpointGroupsDetachEndpointsRequest, err) + } + + return alpha, nil +} + +// ToBeta converts our composite type into an beta type. +// This beta type can be used in GCE API calls. +func (networkEndpointGroupsDetachEndpointsRequest *NetworkEndpointGroupsDetachEndpointsRequest) ToBeta() (*computebeta.NetworkEndpointGroupsDetachEndpointsRequest, error) { + beta := &computebeta.NetworkEndpointGroupsDetachEndpointsRequest{} + err := copyViaJSON(beta, networkEndpointGroupsDetachEndpointsRequest) + if err != nil { + return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", networkEndpointGroupsDetachEndpointsRequest, err) + } + + return beta, nil +} + +// ToGA converts our composite type into an ga type. +// This ga type can be used in GCE API calls. +func (networkEndpointGroupsDetachEndpointsRequest *NetworkEndpointGroupsDetachEndpointsRequest) ToGA() (*compute.NetworkEndpointGroupsDetachEndpointsRequest, error) { + ga := &compute.NetworkEndpointGroupsDetachEndpointsRequest{} + err := copyViaJSON(ga, networkEndpointGroupsDetachEndpointsRequest) + if err != nil { + return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", networkEndpointGroupsDetachEndpointsRequest, err) } return ga, nil @@ -3387,7 +3889,6 @@ func GetSslCertificate(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) if err != nil { return nil, err } - compositeType.Version = version return compositeType, nil } @@ -3443,7 +3944,7 @@ func ToSslCertificateList(objs interface{}) ([]*SslCertificate, error) { err := copyViaJSON(&result, objs) if err != nil { - return nil, fmt.Errorf("could not copy object %v to list of SslCertificate via JSON: %v", objs, err) + return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err) } return result, nil } @@ -3454,7 +3955,7 @@ func ToSslCertificate(obj interface{}) (*SslCertificate, error) { sslCertificate := &SslCertificate{} err := copyViaJSON(sslCertificate, obj) if err != nil { - return nil, fmt.Errorf("could not copy object %+v to SslCertificate via JSON: %v", obj, err) + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, sslCertificate, err) } return sslCertificate, nil @@ -3466,7 +3967,7 @@ func (sslCertificate *SslCertificate) ToAlpha() (*computealpha.SslCertificate, e alpha := &computealpha.SslCertificate{} err := copyViaJSON(alpha, sslCertificate) if err != nil { - return nil, fmt.Errorf("error converting SslCertificate to compute alpha type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", sslCertificate, err) } return alpha, nil @@ -3478,7 +3979,7 @@ func (sslCertificate *SslCertificate) ToBeta() (*computebeta.SslCertificate, err beta := &computebeta.SslCertificate{} err := copyViaJSON(beta, sslCertificate) if err != nil { - return nil, fmt.Errorf("error converting SslCertificate to compute beta type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", sslCertificate, err) } return beta, nil @@ -3490,7 +3991,7 @@ func (sslCertificate *SslCertificate) ToGA() (*compute.SslCertificate, error) { ga := &compute.SslCertificate{} err := copyViaJSON(ga, sslCertificate) if err != nil { - return nil, fmt.Errorf("error converting SslCertificate to compute ga type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", sslCertificate, err) } return ga, nil @@ -3607,7 +4108,6 @@ func GetTargetHttpProxy(gceCloud *gce.Cloud, key *meta.Key, version meta.Version if err != nil { return nil, err } - compositeType.Version = version return compositeType, nil } @@ -3663,7 +4163,7 @@ func ToTargetHttpProxyList(objs interface{}) ([]*TargetHttpProxy, error) { err := copyViaJSON(&result, objs) if err != nil { - return nil, fmt.Errorf("could not copy object %v to list of TargetHttpProxy via JSON: %v", objs, err) + return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err) } return result, nil } @@ -3674,7 +4174,7 @@ func ToTargetHttpProxy(obj interface{}) (*TargetHttpProxy, error) { targetHttpProxy := &TargetHttpProxy{} err := copyViaJSON(targetHttpProxy, obj) if err != nil { - return nil, fmt.Errorf("could not copy object %+v to TargetHttpProxy via JSON: %v", obj, err) + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, targetHttpProxy, err) } return targetHttpProxy, nil @@ -3686,7 +4186,7 @@ func (targetHttpProxy *TargetHttpProxy) ToAlpha() (*computealpha.TargetHttpProxy alpha := &computealpha.TargetHttpProxy{} err := copyViaJSON(alpha, targetHttpProxy) if err != nil { - return nil, fmt.Errorf("error converting TargetHttpProxy to compute alpha type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", targetHttpProxy, err) } return alpha, nil @@ -3698,7 +4198,7 @@ func (targetHttpProxy *TargetHttpProxy) ToBeta() (*computebeta.TargetHttpProxy, beta := &computebeta.TargetHttpProxy{} err := copyViaJSON(beta, targetHttpProxy) if err != nil { - return nil, fmt.Errorf("error converting TargetHttpProxy to compute beta type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", targetHttpProxy, err) } return beta, nil @@ -3710,7 +4210,7 @@ func (targetHttpProxy *TargetHttpProxy) ToGA() (*compute.TargetHttpProxy, error) ga := &compute.TargetHttpProxy{} err := copyViaJSON(ga, targetHttpProxy) if err != nil { - return nil, fmt.Errorf("error converting TargetHttpProxy to compute ga type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", targetHttpProxy, err) } return ga, nil @@ -3827,7 +4327,6 @@ func GetTargetHttpsProxy(gceCloud *gce.Cloud, key *meta.Key, version meta.Versio if err != nil { return nil, err } - compositeType.Version = version return compositeType, nil } @@ -3883,7 +4382,7 @@ func ToTargetHttpsProxyList(objs interface{}) ([]*TargetHttpsProxy, error) { err := copyViaJSON(&result, objs) if err != nil { - return nil, fmt.Errorf("could not copy object %v to list of TargetHttpsProxy via JSON: %v", objs, err) + return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err) } return result, nil } @@ -3894,7 +4393,7 @@ func ToTargetHttpsProxy(obj interface{}) (*TargetHttpsProxy, error) { targetHttpsProxy := &TargetHttpsProxy{} err := copyViaJSON(targetHttpsProxy, obj) if err != nil { - return nil, fmt.Errorf("could not copy object %+v to TargetHttpsProxy via JSON: %v", obj, err) + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, targetHttpsProxy, err) } return targetHttpsProxy, nil @@ -3906,7 +4405,7 @@ func (targetHttpsProxy *TargetHttpsProxy) ToAlpha() (*computealpha.TargetHttpsPr alpha := &computealpha.TargetHttpsProxy{} err := copyViaJSON(alpha, targetHttpsProxy) if err != nil { - return nil, fmt.Errorf("error converting TargetHttpsProxy to compute alpha type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", targetHttpsProxy, err) } return alpha, nil @@ -3918,7 +4417,7 @@ func (targetHttpsProxy *TargetHttpsProxy) ToBeta() (*computebeta.TargetHttpsProx beta := &computebeta.TargetHttpsProxy{} err := copyViaJSON(beta, targetHttpsProxy) if err != nil { - return nil, fmt.Errorf("error converting TargetHttpsProxy to compute beta type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", targetHttpsProxy, err) } return beta, nil @@ -3930,7 +4429,7 @@ func (targetHttpsProxy *TargetHttpsProxy) ToGA() (*compute.TargetHttpsProxy, err ga := &compute.TargetHttpsProxy{} err := copyViaJSON(ga, targetHttpsProxy) if err != nil { - return nil, fmt.Errorf("error converting TargetHttpsProxy to compute ga type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", targetHttpsProxy, err) } return ga, nil @@ -4088,7 +4587,6 @@ func GetUrlMap(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) (*UrlMa if err != nil { return nil, err } - compositeType.Version = version return compositeType, nil } @@ -4144,7 +4642,7 @@ func ToUrlMapList(objs interface{}) ([]*UrlMap, error) { err := copyViaJSON(&result, objs) if err != nil { - return nil, fmt.Errorf("could not copy object %v to list of UrlMap via JSON: %v", objs, err) + return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err) } return result, nil } @@ -4155,7 +4653,7 @@ func ToUrlMap(obj interface{}) (*UrlMap, error) { urlMap := &UrlMap{} err := copyViaJSON(urlMap, obj) if err != nil { - return nil, fmt.Errorf("could not copy object %+v to UrlMap via JSON: %v", obj, err) + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, urlMap, err) } return urlMap, nil @@ -4167,7 +4665,7 @@ func (urlMap *UrlMap) ToAlpha() (*computealpha.UrlMap, error) { alpha := &computealpha.UrlMap{} err := copyViaJSON(alpha, urlMap) if err != nil { - return nil, fmt.Errorf("error converting UrlMap to compute alpha type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", urlMap, err) } return alpha, nil @@ -4179,7 +4677,7 @@ func (urlMap *UrlMap) ToBeta() (*computebeta.UrlMap, error) { beta := &computebeta.UrlMap{} err := copyViaJSON(beta, urlMap) if err != nil { - return nil, fmt.Errorf("error converting UrlMap to compute beta type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", urlMap, err) } return beta, nil @@ -4191,7 +4689,7 @@ func (urlMap *UrlMap) ToGA() (*compute.UrlMap, error) { ga := &compute.UrlMap{} err := copyViaJSON(ga, urlMap) if err != nil { - return nil, fmt.Errorf("error converting UrlMap to compute ga type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", urlMap, err) } return ga, nil diff --git a/pkg/composite/gen/main.go b/pkg/composite/gen/main.go index 779300644e..bb8cee969a 100644 --- a/pkg/composite/gen/main.go +++ b/pkg/composite/gen/main.go @@ -170,7 +170,7 @@ func genTypes(wr io.Writer) { {{- .Name}} {{.GoType}} {{$backtick}}json:"{{.JsonName}},omitempty"{{$backtick}} {{- end}} {{- end}} - {{- if .IsMainService}} + {{- if and .IsMainService .HasCRUD}} googleapi.ServerResponse {{$backtick}}json:"-"{{$backtick}} {{- end}} ForceSendFields []string {{$backtick}}json:"-"{{$backtick}} @@ -195,263 +195,41 @@ func genTypes(wr io.Writer) { // that ignores the name field func genFuncs(wr io.Writer) { const text = ` +{{/* regionalKeyFiller denotes the keyword to use when invoking the regional API for the resource*/}} +{{$regionalKeyFiller := ""}} +{{/* globalKeyFiller denotes the keyword to use when invoking the global API for the resource*/}} +{{$globalKeyFiller := ""}} +{{$onlyZonalKeySupported := false}} + {{$All := .All}} {{$Versions := .Versions}} {{range $type := $All}} -{{if .IsMainService}} - {{if .IsDefaultRegionalService}} - func Create{{.Name}}(gceCloud *gce.Cloud, key *meta.Key, {{.VarName}} *{{.Name}}) error { - ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("{{.Name}}", "create", key.Region, key.Zone, string({{.VarName}}.Version)) - - switch {{.VarName}}.Version { - case meta.VersionAlpha: - alpha, err := {{.VarName}}.ToAlpha() - if err != nil { - return err - } - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Creating alpha region {{.Name}} %v", alpha.Name) - alpha.Region = key.Region - return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Insert(ctx, key, alpha)) - default: - klog.V(3).Infof("Creating alpha {{.Name}} %v", alpha.Name) - return mc.Observe(gceCloud.Compute().AlphaGlobal{{.GetCloudProviderName}}().Insert(ctx, key, alpha)) - } - case meta.VersionBeta: - beta, err := {{.VarName}}.ToBeta() - if err != nil { - return err - } - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Creating beta region {{.Name}} %v", beta.Name) - return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().Insert(ctx, key, beta)) - default: - klog.V(3).Infof("Creating beta {{.Name}} %v", beta.Name) - return mc.Observe(gceCloud.Compute().BetaGlobal{{.GetCloudProviderName}}().Insert(ctx, key, beta)) - } - default: - ga, err := {{.VarName}}.ToGA() - if err != nil { - return err - } - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Creating ga region {{.Name}} %v", ga.Name) - return mc.Observe(gceCloud.Compute().{{.GetCloudProviderName}}().Insert(ctx, key, ga)) - default: - klog.V(3).Infof("Creating ga {{.Name}} %v", ga.Name) - return mc.Observe(gceCloud.Compute().Global{{.GetCloudProviderName}}().Insert(ctx, key, ga)) - }} -} - -{{if .HasUpdate}} -func Update{{.Name}}(gceCloud *gce.Cloud, key *meta.Key, {{.VarName}} *{{.Name}}) error { - ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("{{.Name}}", "update", key.Region, key.Zone, string({{.VarName}}.Version)) - - switch {{.VarName}}.Version { - case meta.VersionAlpha: - alpha, err := {{.VarName}}.ToAlpha() - if err != nil { - return err - } - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Updating alpha region {{.Name}} %v", alpha.Name) - return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Update(ctx, key, alpha)) - default: - klog.V(3).Infof("Updating alpha {{.Name}} %v", alpha.Name) - return mc.Observe(gceCloud.Compute().AlphaGlobal{{.GetCloudProviderName}}().Update(ctx, key, alpha)) - } - case meta.VersionBeta: - beta, err := {{.VarName}}.ToBeta() - if err != nil { - return err - } - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Updating beta region {{.Name}} %v", beta.Name) - return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().Update(ctx, key, beta)) - default: - klog.V(3).Infof("Updating beta {{.Name}} %v", beta.Name) - return mc.Observe(gceCloud.Compute().BetaGlobal{{.GetCloudProviderName}}().Update(ctx, key, beta)) - } default: - ga, err := {{.VarName}}.ToGA() - if err != nil { - return err - } - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Updating ga region {{.Name}} %v", ga.Name) - return mc.Observe(gceCloud.Compute().{{.GetCloudProviderName}}().Update(ctx, key, ga)) - default: - klog.V(3).Infof("Updating ga {{.Name}} %v", ga.Name) - return mc.Observe(gceCloud.Compute().Global{{.GetCloudProviderName}}().Update(ctx, key, ga)) - } - } -} -{{- end}} - -func Get{{.Name}}(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) (*{{.Name}}, error) { - ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("{{.Name}}", "get", key.Region, key.Zone, string(version)) - - var gceObj interface{} - var err error - switch version { - case meta.VersionAlpha: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Getting alpha region {{.Name}} %v", key.Name) - gceObj, err = gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Get(ctx, key) - default: - klog.V(3).Infof("Getting alpha {{.Name}} %v", key.Name) - gceObj, err = gceCloud.Compute().AlphaGlobal{{.GetCloudProviderName}}().Get(ctx, key) - } - case meta.VersionBeta: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Getting beta region {{.Name}} %v", key.Name) - gceObj, err = gceCloud.Compute().Beta{{.GetCloudProviderName}}().Get(ctx, key) - default: - klog.V(3).Infof("Getting beta {{.Name}} %v", key.Name) - gceObj, err = gceCloud.Compute().BetaGlobal{{.GetCloudProviderName}}().Get(ctx, key) - } - default: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Getting ga region {{.Name}} %v", key.Name) - gceObj, err = gceCloud.Compute().{{.GetCloudProviderName}}().Get(ctx, key) - default: - klog.V(3).Infof("Getting ga {{.Name}} %v", key.Name) - gceObj, err = gceCloud.Compute().Global{{.GetCloudProviderName}}().Get(ctx, key) - } - } - if err != nil { - return nil, mc.Observe(err) - } - compositeType, err := To{{.Name}}(gceObj) - if err != nil { - return nil, err - } - - if key.Type() == meta.Regional { - compositeType.Scope = meta.Regional - } - - compositeType.Version = version - return compositeType, nil -} - -func List{{.GetCloudProviderName}}(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) ([]*{{.Name}}, error) { - ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("{{.Name}}", "get", key.Region, key.Zone, string(version)) - - var gceObjs interface{} - var err error - switch version { - case meta.VersionAlpha: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Listing alpha region {{.Name}}") - gceObjs, err = gceCloud.Compute().Alpha{{.GetCloudProviderName}}().List(ctx, key.Region, filter.None) - default: - klog.V(3).Infof("Listing alpha {{.Name}}") - gceObjs, err = gceCloud.Compute().AlphaGlobal{{.GetCloudProviderName}}().List(ctx, filter.None) - } - case meta.VersionBeta: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Listing beta region {{.Name}}") - gceObjs, err = gceCloud.Compute().Beta{{.GetCloudProviderName}}().List(ctx, key.Region, filter.None) - default: - klog.V(3).Infof("Listing beta {{.Name}}") - gceObjs, err = gceCloud.Compute().BetaGlobal{{.GetCloudProviderName}}().List(ctx, filter.None) - } - default: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Listing ga region {{.Name}}") - gceObjs, err = gceCloud.Compute().{{.GetCloudProviderName}}().List(ctx, key.Region, filter.None) - default: - klog.V(3).Infof("Listing ga {{.Name}}") - gceObjs, err = gceCloud.Compute().Global{{.GetCloudProviderName}}().List(ctx, filter.None) - } - } - if err != nil { - return nil, mc.Observe(err) - } - compositeObjs, err := To{{.Name}}List(gceObjs) - if err != nil { - return nil, err - } - for _, obj := range compositeObjs { - obj.Version = version - } - return compositeObjs, nil -} - -func Delete{{.Name}}(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) error { +{{if .IsDefaultZonalService}} + {{$onlyZonalKeySupported = true}} +{{else if .IsDefaultRegionalService}} + {{$regionalKeyFiller = ""}} + {{$globalKeyFiller = "Global"}} + {{$onlyZonalKeySupported = false}} +{{else}} + {{$regionalKeyFiller = "Region"}} + {{$globalKeyFiller = ""}} + {{$onlyZonalKeySupported = false}} +{{- end}} {{/* IsDefaultZonalService */}} + {{if .IsMainService}} + {{if .HasCRUD}} +func Create{{.Name}}(gceCloud *gce.Cloud, key *meta.Key, {{.VarName}} *{{.Name}}) error { ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("{{.Name}}", "delete", key.Region, key.Zone, string(version)) + defer cancel() + mc := compositemetrics.NewMetricContext("{{.Name}}", "create", key.Region, key.Zone, string({{.VarName}}.Version)) - switch version { - case meta.VersionAlpha: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Deleting alpha region {{.Name}} %v", key.Name) - return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Delete(ctx, key)) - default: - klog.V(3).Infof("Deleting alpha {{.Name}} %v", key.Name) - return mc.Observe(gceCloud.Compute().AlphaGlobal{{.GetCloudProviderName}}().Delete(ctx, key)) - } - case meta.VersionBeta: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Deleting beta region {{.Name}} %v", key.Name) - return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().Delete(ctx, key)) - default: - klog.V(3).Infof("Deleting beta {{.Name}} %v", key.Name) - return mc.Observe(gceCloud.Compute().BetaGlobal{{.GetCloudProviderName}}().Delete(ctx, key)) - } + {{- if $onlyZonalKeySupported}} + switch key.Type() { + case meta.Zonal: default: - switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Deleting ga region {{.Name}} %v", key.Name) - return mc.Observe(gceCloud.Compute().{{.GetCloudProviderName}}().Delete(ctx, key)) - default: - klog.V(3).Infof("Deleting ga {{.Name}} %v", key.Name) - return mc.Observe(gceCloud.Compute().Global{{.GetCloudProviderName}}().Delete(ctx, key)) - } + return fmt.Errorf("Key %v not valid for zonal resource {{.Name}} %v", key, key.Name) } -} - -// To{{.Name}}List converts a list of compute alpha, beta or GA -// {{.Name}} into a list of our composite type. -func To{{.Name}}List(objs interface{}) ([]*{{.Name}}, error) { - result := []*{{.Name}}{} - - err := copyViaJSON(&result, objs) - if err != nil { - return nil, fmt.Errorf("Could not copy object %v to list of {{.Name}} via JSON: %v", objs, err) - } - return result, nil -} - -{{ else }} -func Create{{.Name}}(gceCloud *gce.Cloud, key *meta.Key, {{.VarName}} *{{.Name}}) error { - ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("{{.Name}}", "create", key.Region, key.Zone, string({{.VarName}}.Version)) + {{- end}} {{/* $onlyZonalKeySupported*/}} switch {{.VarName}}.Version { case meta.VersionAlpha: @@ -459,183 +237,346 @@ func Create{{.Name}}(gceCloud *gce.Cloud, key *meta.Key, {{.VarName}} *{{.Name}} if err != nil { return err } + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Creating alpha zonal {{.Name}} %v", alpha.Name) + return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Insert(ctx, key, alpha)) + {{- else}} switch key.Type() { case meta.Regional: - klog.V(3).Infof("Creating alpha region {{.Name}} %v", alpha.Name) + klog.V(3).Infof("Creating alpha region {{.Name}} %v", alpha.Name) alpha.Region = key.Region - return mc.Observe(gceCloud.Compute().AlphaRegion{{.GetCloudProviderName}}().Insert(ctx, key, alpha)) + return mc.Observe(gceCloud.Compute().Alpha{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Insert(ctx, key, alpha)) default: - klog.V(3).Infof("Creating alpha {{.Name}} %v", alpha.Name) - return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Insert(ctx, key, alpha)) - } + klog.V(3).Infof("Creating alpha {{.Name}} %v", alpha.Name) + return mc.Observe(gceCloud.Compute().Alpha{{$globalKeyFiller}}{{.GetCloudProviderName}}().Insert(ctx, key, alpha)) + } + {{- end}} {{/* $onlyZonalKeySupported*/}} case meta.VersionBeta: beta, err := {{.VarName}}.ToBeta() if err != nil { return err } + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Creating beta zonal {{.Name}} %v", beta.Name) + return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().Insert(ctx, key, beta)) + {{- else}} switch key.Type() { case meta.Regional: - klog.V(3).Infof("Creating beta region {{.Name}} %v", beta.Name) + klog.V(3).Infof("Creating beta region {{.Name}} %v", beta.Name) beta.Region = key.Region - return mc.Observe(gceCloud.Compute().BetaRegion{{.GetCloudProviderName}}().Insert(ctx, key, beta)) + return mc.Observe(gceCloud.Compute().Beta{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Insert(ctx, key, beta)) default: - klog.V(3).Infof("Creating beta {{.Name}} %v", beta.Name) - return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().Insert(ctx, key, beta)) - } + klog.V(3).Infof("Creating beta {{.Name}} %v", beta.Name) + return mc.Observe(gceCloud.Compute().Beta{{$globalKeyFiller}}{{.GetCloudProviderName}}().Insert(ctx, key, beta)) + } + {{- end}} {{/* $onlyZonalKeySupported*/}} default: ga, err := {{.VarName}}.ToGA() if err != nil { return err } + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Creating ga zonal {{.Name}} %v", ga.Name) + return mc.Observe(gceCloud.Compute().{{.GetCloudProviderName}}().Insert(ctx, key, ga)) + {{- else if .IsDefaultRegionalService}} {{/* In GA, only Global api exists, no Region */}} + switch key.Type() { + case meta.Regional: + klog.V(3).Infof("Creating ga region {{.Name}} %v", ga.Name) + ga.Region = key.Region + return mc.Observe(gceCloud.Compute().{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Insert(ctx, key, ga)) + default: + klog.V(3).Infof("Creating ga {{.Name}} %v", ga.Name) + return mc.Observe(gceCloud.Compute().{{$globalKeyFiller}}{{.GetCloudProviderName}}().Insert(ctx, key, ga)) + } + {{- else}} klog.V(3).Infof("Creating ga {{.Name}} %v", ga.Name) return mc.Observe(gceCloud.Compute().{{.GetCloudProviderName}}().Insert(ctx, key, ga)) + {{- end}} {{/* $onlyZonalKeySupported*/}} } } {{if .HasUpdate}} func Update{{.Name}}(gceCloud *gce.Cloud, key *meta.Key, {{.VarName}} *{{.Name}}) error { ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("{{.Name}}", "update", key.Region, key.Zone, string({{.VarName}}.Version)) + defer cancel() + mc := compositemetrics.NewMetricContext("{{.Name}}", "update", key.Region, key.Zone, string({{.VarName}}.Version)) + + {{- if $onlyZonalKeySupported}} + switch key.Type() { + case meta.Zonal: + default: + return fmt.Errorf("Key %v not valid for zonal resource {{.Name}} %v", key, key.Name) + } + + {{- end}} {{/* $onlyZonalKeySupported*/}} switch {{.VarName}}.Version { case meta.VersionAlpha: alpha, err := {{.VarName}}.ToAlpha() if err != nil { return err } + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Updating alpha zonal {{.Name}} %v", alpha.Name) + return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Update(ctx, key, alpha)) + {{- else}} switch key.Type() { case meta.Regional: - klog.V(3).Infof("Updating alpha region {{.Name}} %v", alpha.Name) - return mc.Observe(gceCloud.Compute().AlphaRegion{{.GetCloudProviderName}}().Update(ctx, key, alpha)) + klog.V(3).Infof("Updating alpha region {{.Name}} %v", alpha.Name) + return mc.Observe(gceCloud.Compute().Alpha{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Update(ctx, key, alpha)) default: - klog.V(3).Infof("Updating alpha {{.Name}} %v", alpha.Name) - return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Update(ctx, key, alpha)) + klog.V(3).Infof("Updating alpha {{.Name}} %v", alpha.Name) + return mc.Observe(gceCloud.Compute().Alpha{{$globalKeyFiller}}{{.GetCloudProviderName}}().Update(ctx, key, alpha)) } + {{- end}} {{/* $onlyZonalKeySupported*/}} case meta.VersionBeta: beta, err := {{.VarName}}.ToBeta() if err != nil { return err } + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Updating beta zonal {{.Name}} %v", beta.Name) + return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().Update(ctx, key, beta)) + {{- else}} switch key.Type() { case meta.Regional: klog.V(3).Infof("Updating beta region {{.Name}} %v", beta.Name) - return mc.Observe(gceCloud.Compute().BetaRegion{{.GetCloudProviderName}}().Update(ctx, key, beta)) + return mc.Observe(gceCloud.Compute().Beta{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Update(ctx, key, beta)) default: klog.V(3).Infof("Updating beta {{.Name}} %v", beta.Name) - return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().Update(ctx, key, beta)) + return mc.Observe(gceCloud.Compute().Beta{{$globalKeyFiller}}{{.GetCloudProviderName}}().Update(ctx, key, beta)) } + {{- end}} {{/* $onlyZonalKeySupported*/}} default: ga, err := {{.VarName}}.ToGA() if err != nil { return err } - klog.V(3).Infof("Updating ga {{.Name}} %v", ga.Name) + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Updating ga zonal {{.Name}} %v", ga.Name) return mc.Observe(gceCloud.Compute().{{.GetCloudProviderName}}().Update(ctx, key, ga)) + {{- else if .IsDefaultRegionalService}} {{/* In GA, only Global api exists, no Region */}} + switch key.Type() { + case meta.Regional: + klog.V(3).Infof("Updating ga region {{.Name}} %v", ga.Name) + return mc.Observe(gceCloud.Compute().{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Update(ctx, key, ga)) + default: + klog.V(3).Infof("Updating ga {{.Name}} %v", ga.Name) + return mc.Observe(gceCloud.Compute().{{$globalKeyFiller}}{{.GetCloudProviderName}}().Update(ctx, key, ga)) + } + {{- else}} + klog.V(3).Infof("Updating ga {{.Name}} %v", ga.Name) + return mc.Observe(gceCloud.Compute().{{$globalKeyFiller}}{{.GetCloudProviderName}}().Update(ctx, key, ga)) + {{- end}} {{/* $onlyZonalKeySupported*/}} } } -{{- end}} +{{- end}} {{/*HasUpdate*/}} func Delete{{.Name}}(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) error { ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("{{.Name}}", "delete", key.Region, key.Zone, string(version)) + defer cancel() + mc := compositemetrics.NewMetricContext("{{.Name}}", "delete", key.Region, key.Zone, string(version)) + + {{- if $onlyZonalKeySupported}} + switch key.Type() { + case meta.Zonal: + default: + return fmt.Errorf("Key %v not valid for zonal resource {{.Name}} %v", key, key.Name) + } + {{- end}} {{/* $onlyZonalKeySupported*/}} switch version { case meta.VersionAlpha: + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Deleting alpha zonal {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Delete(ctx, key)) + {{- else}} switch key.Type() { case meta.Regional: - klog.V(3).Infof("Deleting alpha region {{.Name}} %v", key.Name) - return mc.Observe(gceCloud.Compute().AlphaRegion{{.GetCloudProviderName}}().Delete(ctx, key)) + klog.V(3).Infof("Deleting alpha region {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().Alpha{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Delete(ctx, key)) default: - klog.V(3).Infof("Deleting alpha {{.Name}} %v", key.Name) - return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Delete(ctx, key)) + klog.V(3).Infof("Deleting alpha {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().Alpha{{$globalKeyFiller}}{{.GetCloudProviderName}}().Delete(ctx, key)) } + {{- end}} {{/* $onlyZonalKeySupported*/}} case meta.VersionBeta: + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Deleting beta zonal {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().Delete(ctx, key)) + {{- else}} switch key.Type() { case meta.Regional: klog.V(3).Infof("Deleting beta region {{.Name}} %v", key.Name) - return mc.Observe(gceCloud.Compute().BetaRegion{{.GetCloudProviderName}}().Delete(ctx, key)) + return mc.Observe(gceCloud.Compute().Beta{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Delete(ctx, key)) default: klog.V(3).Infof("Deleting beta {{.Name}} %v", key.Name) - return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().Delete(ctx, key)) + return mc.Observe(gceCloud.Compute().Beta{{$globalKeyFiller}}{{.GetCloudProviderName}}().Delete(ctx, key)) } + {{- end}} {{/* $onlyZonalKeySupported*/}} default: - klog.V(3).Infof("Deleting ga {{.Name}} %v", key.Name) + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Deleting ga zonal {{.Name}} %v", key.Name) return mc.Observe(gceCloud.Compute().{{.GetCloudProviderName}}().Delete(ctx, key)) + {{- else if .IsDefaultRegionalService}} + switch key.Type() { + case meta.Regional: + klog.V(3).Infof("Deleting ga region {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Delete(ctx, key)) + default: + klog.V(3).Infof("Deleting ga {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().{{$globalKeyFiller}}{{.GetCloudProviderName}}().Delete(ctx, key)) + } + {{- else}} + klog.V(3).Infof("Deleting ga {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().{{$globalKeyFiller}}{{.GetCloudProviderName}}().Delete(ctx, key)) + {{- end}} {{/* $onlyZonalKeySupported*/}} } } func Get{{.Name}}(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) (*{{.Name}}, error) { ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("{{.Name}}", "get", key.Region, key.Zone, string(version)) + defer cancel() + mc := compositemetrics.NewMetricContext("{{.Name}}", "get", key.Region, key.Zone, string(version)) var gceObj interface{} var err error + + {{- if $onlyZonalKeySupported}} + switch key.Type() { + case meta.Zonal: + default: + return nil, fmt.Errorf("Key %v not valid for zonal resource {{.Name}} %v", key, key.Name) + } + + {{- end}} {{/* $onlyZonalKeySupported*/}} switch version { case meta.VersionAlpha: + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Getting alpha zonal {{.Name}} %v", key.Name) + gceObj, err = gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Get(ctx, key) + {{- else}} switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Getting alpha region {{.Name}} %v", key.Name) - gceObj, err = gceCloud.Compute().AlphaRegion{{.GetCloudProviderName}}().Get(ctx, key) - default: + case meta.Regional: + klog.V(3).Infof("Getting alpha region {{.Name}} %v", key.Name) + gceObj, err = gceCloud.Compute().Alpha{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Get(ctx, key) + default: klog.V(3).Infof("Getting alpha {{.Name}} %v", key.Name) - gceObj, err = gceCloud.Compute().Alpha{{.GetCloudProviderName}}().Get(ctx, key) + gceObj, err = gceCloud.Compute().Alpha{{$globalKeyFiller}}{{.GetCloudProviderName}}().Get(ctx, key) } + {{- end}} {{/* $onlyZonalKeySupported*/}} case meta.VersionBeta: + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Getting beta zonal {{.Name}} %v", key.Name) + gceObj, err = gceCloud.Compute().Beta{{.GetCloudProviderName}}().Get(ctx, key) + {{else}} switch key.Type() { - case meta.Regional: + case meta.Regional: klog.V(3).Infof("Getting beta region {{.Name}} %v", key.Name) - gceObj, err = gceCloud.Compute().BetaRegion{{.GetCloudProviderName}}().Get(ctx, key) - default: + gceObj, err = gceCloud.Compute().Beta{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Get(ctx, key) + default: klog.V(3).Infof("Getting beta {{.Name}} %v", key.Name) - gceObj, err = gceCloud.Compute().Beta{{.GetCloudProviderName}}().Get(ctx, key) + gceObj, err = gceCloud.Compute().Beta{{$globalKeyFiller}}{{.GetCloudProviderName}}().Get(ctx, key) } + {{- end}} {{/* $onlyZonalKeySupported*/}} default: - klog.V(3).Infof("Getting ga {{.Name}} %v", key.Name) + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Getting ga zonal {{.Name}} %v", key.Name) gceObj, err = gceCloud.Compute().{{.GetCloudProviderName}}().Get(ctx, key) + {{- else if .IsDefaultRegionalService}}{{/* In GA, only Global api exists, no Region */}} + switch key.Type() { + case meta.Regional: + klog.V(3).Infof("Getting ga region {{.Name}} %v", key.Name) + gceObj, err = gceCloud.Compute().{{$regionalKeyFiller}}{{.GetCloudProviderName}}().Get(ctx, key) + default: + klog.V(3).Infof("Getting ga {{.Name}} %v", key.Name) + gceObj, err = gceCloud.Compute().{{$globalKeyFiller}}{{.GetCloudProviderName}}().Get(ctx, key) + } + {{- else}} + klog.V(3).Infof("Getting ga {{.Name}} %v", key.Name) + gceObj, err = gceCloud.Compute().{{$globalKeyFiller}}{{.GetCloudProviderName}}().Get(ctx, key) + {{- end}} {{/* $onlyZonalKeySupported*/}} } if err != nil { return nil, mc.Observe(err) } compositeType, err := To{{.Name}}(gceObj) - if err != nil { - return nil, err - } + if err != nil { + return nil, err + } - compositeType.Version = version - return compositeType, nil + {{- if $onlyZonalKeySupported}} + compositeType.Scope = meta.Zonal + {{- else if .IsDefaultRegionalService}} + if key.Type() == meta.Regional { + compositeType.Scope = meta.Regional + } + {{- end}} {{/* $onlyZonalKeySupported*/}} + compositeType.Version = version + return compositeType, nil } func List{{.GetCloudProviderName}}(gceCloud *gce.Cloud, key *meta.Key, version meta.Version) ([]*{{.Name}}, error) { ctx, cancel := cloudprovider.ContextWithCallTimeout() - defer cancel() - mc := compositemetrics.NewMetricContext("{{.Name}}", "get", key.Region, key.Zone, string(version)) + defer cancel() + mc := compositemetrics.NewMetricContext("{{.Name}}", "get", key.Region, key.Zone, string(version)) var gceObjs interface{} var err error + + {{- if $onlyZonalKeySupported}} + switch key.Type() { + case meta.Zonal: + default: + return nil, fmt.Errorf("Key %v not valid for zonal resource {{.Name}} %v", key, key.Name) + } + + {{- end}} {{/* $onlyZonalKeySupported*/}} switch version { case meta.VersionAlpha: + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Listing alpha zone{{.Name}}") + gceObjs, err = gceCloud.Compute().Alpha{{.GetCloudProviderName}}().List(ctx, key.Zone, filter.None) + {{- else}} switch key.Type() { - case meta.Regional: - klog.V(3).Infof("Listing alpha region {{.Name}}") - gceObjs, err = gceCloud.Compute().AlphaRegion{{.GetCloudProviderName}}().List(ctx, key.Region, filter.None) - default: + case meta.Regional: + klog.V(3).Infof("Listing alpha region {{.Name}}") + gceObjs, err = gceCloud.Compute().Alpha{{$regionalKeyFiller}}{{.GetCloudProviderName}}().List(ctx, key.Region, filter.None) + default: klog.V(3).Infof("Listing alpha {{.Name}}") - gceObjs, err = gceCloud.Compute().Alpha{{.GetCloudProviderName}}().List(ctx, filter.None) + gceObjs, err = gceCloud.Compute().Alpha{{$globalKeyFiller}}{{.GetCloudProviderName}}().List(ctx, filter.None) } + {{- end}} {{/* $onlyZonalKeySupported*/}} case meta.VersionBeta: + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Listing beta zone{{.Name}}") + gceObjs, err = gceCloud.Compute().Beta{{.GetCloudProviderName}}().List(ctx, key.Zone, filter.None) + {{- else}} switch key.Type() { - case meta.Regional: + case meta.Regional: klog.V(3).Infof("Listing beta region {{.Name}}") - gceObjs, err = gceCloud.Compute().BetaRegion{{.GetCloudProviderName}}().List(ctx, key.Region, filter.None) - default: + gceObjs, err = gceCloud.Compute().Beta{{$regionalKeyFiller}}{{.GetCloudProviderName}}().List(ctx, key.Region, filter.None) + default: klog.V(3).Infof("Listing beta {{.Name}}") - gceObjs, err = gceCloud.Compute().Beta{{.GetCloudProviderName}}().List(ctx, filter.None) + gceObjs, err = gceCloud.Compute().Beta{{$globalKeyFiller}}{{.GetCloudProviderName}}().List(ctx, filter.None) } + {{- end}} {{/* $onlyZonalKeySupported*/}} default: - klog.V(3).Infof("Listing ga {{.Name}}") - gceObjs, err = gceCloud.Compute().{{.GetCloudProviderName}}().List(ctx, filter.None) + {{- if $onlyZonalKeySupported}} + klog.V(3).Infof("Listing ga zone{{.Name}}") + gceObjs, err = gceCloud.Compute().{{.GetCloudProviderName}}().List(ctx, key.Zone, filter.None) + {{- else if .IsDefaultRegionalService}}{{/* In GA, only Global api exists, no Region */}} + switch key.Type() { + case meta.Regional: + klog.V(3).Infof("Listing ga region {{.Name}}") + gceObjs, err = gceCloud.Compute().{{$regionalKeyFiller}}{{.GetCloudProviderName}}().List(ctx, key.Region, filter.None) + default: + klog.V(3).Infof("Listing ga {{.Name}}") + gceObjs, err = gceCloud.Compute().{{$globalKeyFiller}}{{.GetCloudProviderName}}().List(ctx, filter.None) + } + {{- else}} + klog.V(3).Infof("Listing ga {{.Name}}") + gceObjs, err = gceCloud.Compute().{{$globalKeyFiller}}{{.GetCloudProviderName}}().List(ctx, filter.None) + {{- end}} {{/* $onlyZonalKeySupported*/}} } if err != nil { return nil, mc.Observe(err) @@ -651,6 +592,79 @@ func List{{.GetCloudProviderName}}(gceCloud *gce.Cloud, key *meta.Key, version m return compositeObjs, nil } +{{if .IsGroupResourceService}} +func {{.GetGroupResourceInfo.AttachFuncName}}(gceCloud *gce.Cloud, key *meta.Key, version meta.Version, req *{{.GetGroupResourceInfo.AttachReqName}}) error { + ctx, cancel := cloudprovider.ContextWithCallTimeout() + defer cancel() + mc := compositemetrics.NewMetricContext("{{.Name}}", "attach", key.Region, key.Zone, string(version)) + + switch key.Type() { + case meta.Zonal: + default: + return fmt.Errorf("Key %v not valid for zonal resource {{.Name}} %v", key, key.Name) + } + + switch version { + case meta.VersionAlpha: + alphareq, err := req.ToAlpha() + if err != nil { + return err + } + klog.V(3).Infof("Attaching to alpha zonal {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().{{.GetGroupResourceInfo.AttachFuncName}}(ctx, key, alphareq)) + case meta.VersionBeta: + betareq, err := req.ToBeta() + if err != nil { + return err + } + klog.V(3).Infof("Attaching to beta zonal {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().{{.GetGroupResourceInfo.AttachFuncName}}(ctx, key, betareq)) + default: + gareq, err := req.ToGA() + if err != nil { + return err + } + klog.V(3).Infof("Attaching to ga zonal {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().{{.GetCloudProviderName}}().{{.GetGroupResourceInfo.AttachFuncName}}(ctx, key, gareq)) + } +} + +func {{.GetGroupResourceInfo.DetachFuncName}}(gceCloud *gce.Cloud, key *meta.Key, version meta.Version, req *{{.GetGroupResourceInfo.DetachReqName}}) error { + ctx, cancel := cloudprovider.ContextWithCallTimeout() + defer cancel() + mc := compositemetrics.NewMetricContext("{{.Name}}", "attach", key.Region, key.Zone, string(version)) + + switch key.Type() { + case meta.Zonal: + default: + return fmt.Errorf("Key %v not valid for zonal resource {{.Name}} %v", key, key.Name) + } + + switch version { + case meta.VersionAlpha: + alphareq, err := req.ToAlpha() + if err != nil { + return err + } + klog.V(3).Infof("Detaching from alpha zonal {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().Alpha{{.GetCloudProviderName}}().{{.GetGroupResourceInfo.DetachFuncName}}(ctx, key, alphareq)) + case meta.VersionBeta: + betareq, err := req.ToBeta() + if err != nil { + return err + } + klog.V(3).Infof("Detaching from beta zonal {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().Beta{{.GetCloudProviderName}}().{{.GetGroupResourceInfo.DetachFuncName}}(ctx, key, betareq)) + default: + gareq, err := req.ToGA() + if err != nil { + return err + } + klog.V(3).Infof("Detaching from ga zonal {{.Name}} %v", key.Name) + return mc.Observe(gceCloud.Compute().{{.GetCloudProviderName}}().{{.GetGroupResourceInfo.DetachFuncName}}(ctx, key, gareq)) + } +} +{{end}} {{/*IsGroupResourceService*/}} // To{{.Name}}List converts a list of compute alpha, beta or GA // {{.Name}} into a list of our composite type. func To{{.Name}}List(objs interface{}) ([]*{{.Name}}, error) { @@ -658,12 +672,11 @@ func To{{.Name}}List(objs interface{}) ([]*{{.Name}}, error) { err := copyViaJSON(&result, objs) if err != nil { - return nil, fmt.Errorf("could not copy object %v to list of {{.Name}} via JSON: %v", objs, err) + return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err) } return result, nil } - -{{- end}} +{{- end}} {{/*HasCRUD*/}} // To{{.Name}} converts a compute alpha, beta or GA // {{.Name}} into our composite type. @@ -671,7 +684,7 @@ func To{{.Name}}(obj interface{}) (*{{.Name}}, error) { {{.VarName}} := &{{.Name}}{} err := copyViaJSON({{.VarName}}, obj) if err != nil { - return nil, fmt.Errorf("could not copy object %+v to {{.Name}} via JSON: %v", obj, err) + return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, {{.VarName}}, err) } return {{.VarName}}, nil @@ -685,7 +698,7 @@ func ({{$type.VarName}} *{{$type.Name}}) To{{$version}}() (*compute{{$extension} {{$lower}} := &compute{{$extension}}.{{$type.Name}}{} err := copyViaJSON({{$lower}}, {{$type.VarName}}) if err != nil { - return nil, fmt.Errorf("error converting {{$type.Name}} to compute {{$lower}} type via JSON: %v", err) + return nil, fmt.Errorf("error converting %T to compute {{$lower}} type via JSON: %v", {{$type.VarName}}, err) } {{- if eq $type.Name "BackendService"}} @@ -696,7 +709,7 @@ func ({{$type.VarName}} *{{$type.Name}}) To{{$version}}() (*compute{{$extension} if {{$lower}}.Iap != nil { {{$lower}}.Iap.ForceSendFields = []string{"Enabled", "Oauth2ClientId", "Oauth2ClientSecret"} } - {{- end}} + {{- end}} return {{$lower}}, nil } diff --git a/pkg/composite/gen_test.go b/pkg/composite/gen_test.go index 2f10835937..97fad6f782 100644 --- a/pkg/composite/gen_test.go +++ b/pkg/composite/gen_test.go @@ -678,6 +678,319 @@ func TestMutualTls(t *testing.T) { } } +func TestNetworkEndpoint(t *testing.T) { + compositeType := reflect.TypeOf(NetworkEndpoint{}) + alphaType := reflect.TypeOf(computealpha.NetworkEndpoint{}) + if err := typeEquality(compositeType, alphaType, true); err != nil { + t.Fatal(err) + } +} +func TestNetworkEndpointGroup(t *testing.T) { + // Use reflection to verify that our composite type contains all the + // same fields as the alpha type. + compositeType := reflect.TypeOf(NetworkEndpointGroup{}) + alphaType := reflect.TypeOf(computealpha.NetworkEndpointGroup{}) + betaType := reflect.TypeOf(computebeta.NetworkEndpointGroup{}) + gaType := reflect.TypeOf(compute.NetworkEndpointGroup{}) + + // For the composite type, remove the Version field from consideration + compositeTypeNumFields := compositeType.NumField() - 2 + if compositeTypeNumFields != alphaType.NumField() { + t.Fatalf("%v should contain %v fields. Got %v", alphaType.Name(), alphaType.NumField(), compositeTypeNumFields) + } + + // Compare all the fields by doing a lookup since we can't guarantee that they'll be in the same order + // Make sure that composite type is strictly alpha fields + internal bookkeeping + for i := 2; i < compositeType.NumField(); i++ { + lookupField, found := alphaType.FieldByName(compositeType.Field(i).Name) + if !found { + t.Fatal(fmt.Errorf("Field %v not present in alpha type %v", compositeType.Field(i), alphaType)) + } + if err := compareFields(compositeType.Field(i), lookupField); err != nil { + t.Fatal(err) + } + } + + // Verify that all beta fields are in composite type + if err := typeEquality(betaType, compositeType, false); err != nil { + t.Fatal(err) + } + + // Verify that all GA fields are in composite type + if err := typeEquality(gaType, compositeType, false); err != nil { + t.Fatal(err) + } +} + +func TestToNetworkEndpointGroup(t *testing.T) { + testCases := []struct { + input interface{} + expected *NetworkEndpointGroup + }{ + { + computealpha.NetworkEndpointGroup{}, + &NetworkEndpointGroup{}, + }, + { + computebeta.NetworkEndpointGroup{}, + &NetworkEndpointGroup{}, + }, + { + compute.NetworkEndpointGroup{}, + &NetworkEndpointGroup{}, + }, + } + for _, testCase := range testCases { + result, _ := ToNetworkEndpointGroup(testCase.input) + if !reflect.DeepEqual(result, testCase.expected) { + t.Fatalf("ToNetworkEndpointGroup(input) = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(testCase.input), pretty.Sprint(result), pretty.Sprint(testCase.expected)) + } + } +} + +func TestNetworkEndpointGroupToAlpha(t *testing.T) { + composite := NetworkEndpointGroup{} + expected := &computealpha.NetworkEndpointGroup{} + result, err := composite.ToAlpha() + if err != nil { + t.Fatalf("NetworkEndpointGroup.ToAlpha() error: %v", err) + } + + if !reflect.DeepEqual(result, expected) { + t.Fatalf("NetworkEndpointGroup.ToAlpha() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected)) + } +} +func TestNetworkEndpointGroupToBeta(t *testing.T) { + composite := NetworkEndpointGroup{} + expected := &computebeta.NetworkEndpointGroup{} + result, err := composite.ToBeta() + if err != nil { + t.Fatalf("NetworkEndpointGroup.ToBeta() error: %v", err) + } + + if !reflect.DeepEqual(result, expected) { + t.Fatalf("NetworkEndpointGroup.ToBeta() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected)) + } +} +func TestNetworkEndpointGroupToGA(t *testing.T) { + composite := NetworkEndpointGroup{} + expected := &compute.NetworkEndpointGroup{} + result, err := composite.ToGA() + if err != nil { + t.Fatalf("NetworkEndpointGroup.ToGA() error: %v", err) + } + + if !reflect.DeepEqual(result, expected) { + t.Fatalf("NetworkEndpointGroup.ToGA() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected)) + } +} + +func TestNetworkEndpointGroupLbNetworkEndpointGroup(t *testing.T) { + compositeType := reflect.TypeOf(NetworkEndpointGroupLbNetworkEndpointGroup{}) + alphaType := reflect.TypeOf(computealpha.NetworkEndpointGroupLbNetworkEndpointGroup{}) + if err := typeEquality(compositeType, alphaType, true); err != nil { + t.Fatal(err) + } +} +func TestNetworkEndpointGroupsAttachEndpointsRequest(t *testing.T) { + // Use reflection to verify that our composite type contains all the + // same fields as the alpha type. + compositeType := reflect.TypeOf(NetworkEndpointGroupsAttachEndpointsRequest{}) + alphaType := reflect.TypeOf(computealpha.NetworkEndpointGroupsAttachEndpointsRequest{}) + betaType := reflect.TypeOf(computebeta.NetworkEndpointGroupsAttachEndpointsRequest{}) + gaType := reflect.TypeOf(compute.NetworkEndpointGroupsAttachEndpointsRequest{}) + + // For the composite type, remove the Version field from consideration + compositeTypeNumFields := compositeType.NumField() - 2 + if compositeTypeNumFields != alphaType.NumField() { + t.Fatalf("%v should contain %v fields. Got %v", alphaType.Name(), alphaType.NumField(), compositeTypeNumFields) + } + + // Compare all the fields by doing a lookup since we can't guarantee that they'll be in the same order + // Make sure that composite type is strictly alpha fields + internal bookkeeping + for i := 2; i < compositeType.NumField(); i++ { + lookupField, found := alphaType.FieldByName(compositeType.Field(i).Name) + if !found { + t.Fatal(fmt.Errorf("Field %v not present in alpha type %v", compositeType.Field(i), alphaType)) + } + if err := compareFields(compositeType.Field(i), lookupField); err != nil { + t.Fatal(err) + } + } + + // Verify that all beta fields are in composite type + if err := typeEquality(betaType, compositeType, false); err != nil { + t.Fatal(err) + } + + // Verify that all GA fields are in composite type + if err := typeEquality(gaType, compositeType, false); err != nil { + t.Fatal(err) + } +} + +func TestToNetworkEndpointGroupsAttachEndpointsRequest(t *testing.T) { + testCases := []struct { + input interface{} + expected *NetworkEndpointGroupsAttachEndpointsRequest + }{ + { + computealpha.NetworkEndpointGroupsAttachEndpointsRequest{}, + &NetworkEndpointGroupsAttachEndpointsRequest{}, + }, + { + computebeta.NetworkEndpointGroupsAttachEndpointsRequest{}, + &NetworkEndpointGroupsAttachEndpointsRequest{}, + }, + { + compute.NetworkEndpointGroupsAttachEndpointsRequest{}, + &NetworkEndpointGroupsAttachEndpointsRequest{}, + }, + } + for _, testCase := range testCases { + result, _ := ToNetworkEndpointGroupsAttachEndpointsRequest(testCase.input) + if !reflect.DeepEqual(result, testCase.expected) { + t.Fatalf("ToNetworkEndpointGroupsAttachEndpointsRequest(input) = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(testCase.input), pretty.Sprint(result), pretty.Sprint(testCase.expected)) + } + } +} + +func TestNetworkEndpointGroupsAttachEndpointsRequestToAlpha(t *testing.T) { + composite := NetworkEndpointGroupsAttachEndpointsRequest{} + expected := &computealpha.NetworkEndpointGroupsAttachEndpointsRequest{} + result, err := composite.ToAlpha() + if err != nil { + t.Fatalf("NetworkEndpointGroupsAttachEndpointsRequest.ToAlpha() error: %v", err) + } + + if !reflect.DeepEqual(result, expected) { + t.Fatalf("NetworkEndpointGroupsAttachEndpointsRequest.ToAlpha() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected)) + } +} +func TestNetworkEndpointGroupsAttachEndpointsRequestToBeta(t *testing.T) { + composite := NetworkEndpointGroupsAttachEndpointsRequest{} + expected := &computebeta.NetworkEndpointGroupsAttachEndpointsRequest{} + result, err := composite.ToBeta() + if err != nil { + t.Fatalf("NetworkEndpointGroupsAttachEndpointsRequest.ToBeta() error: %v", err) + } + + if !reflect.DeepEqual(result, expected) { + t.Fatalf("NetworkEndpointGroupsAttachEndpointsRequest.ToBeta() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected)) + } +} +func TestNetworkEndpointGroupsAttachEndpointsRequestToGA(t *testing.T) { + composite := NetworkEndpointGroupsAttachEndpointsRequest{} + expected := &compute.NetworkEndpointGroupsAttachEndpointsRequest{} + result, err := composite.ToGA() + if err != nil { + t.Fatalf("NetworkEndpointGroupsAttachEndpointsRequest.ToGA() error: %v", err) + } + + if !reflect.DeepEqual(result, expected) { + t.Fatalf("NetworkEndpointGroupsAttachEndpointsRequest.ToGA() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected)) + } +} +func TestNetworkEndpointGroupsDetachEndpointsRequest(t *testing.T) { + // Use reflection to verify that our composite type contains all the + // same fields as the alpha type. + compositeType := reflect.TypeOf(NetworkEndpointGroupsDetachEndpointsRequest{}) + alphaType := reflect.TypeOf(computealpha.NetworkEndpointGroupsDetachEndpointsRequest{}) + betaType := reflect.TypeOf(computebeta.NetworkEndpointGroupsDetachEndpointsRequest{}) + gaType := reflect.TypeOf(compute.NetworkEndpointGroupsDetachEndpointsRequest{}) + + // For the composite type, remove the Version field from consideration + compositeTypeNumFields := compositeType.NumField() - 2 + if compositeTypeNumFields != alphaType.NumField() { + t.Fatalf("%v should contain %v fields. Got %v", alphaType.Name(), alphaType.NumField(), compositeTypeNumFields) + } + + // Compare all the fields by doing a lookup since we can't guarantee that they'll be in the same order + // Make sure that composite type is strictly alpha fields + internal bookkeeping + for i := 2; i < compositeType.NumField(); i++ { + lookupField, found := alphaType.FieldByName(compositeType.Field(i).Name) + if !found { + t.Fatal(fmt.Errorf("Field %v not present in alpha type %v", compositeType.Field(i), alphaType)) + } + if err := compareFields(compositeType.Field(i), lookupField); err != nil { + t.Fatal(err) + } + } + + // Verify that all beta fields are in composite type + if err := typeEquality(betaType, compositeType, false); err != nil { + t.Fatal(err) + } + + // Verify that all GA fields are in composite type + if err := typeEquality(gaType, compositeType, false); err != nil { + t.Fatal(err) + } +} + +func TestToNetworkEndpointGroupsDetachEndpointsRequest(t *testing.T) { + testCases := []struct { + input interface{} + expected *NetworkEndpointGroupsDetachEndpointsRequest + }{ + { + computealpha.NetworkEndpointGroupsDetachEndpointsRequest{}, + &NetworkEndpointGroupsDetachEndpointsRequest{}, + }, + { + computebeta.NetworkEndpointGroupsDetachEndpointsRequest{}, + &NetworkEndpointGroupsDetachEndpointsRequest{}, + }, + { + compute.NetworkEndpointGroupsDetachEndpointsRequest{}, + &NetworkEndpointGroupsDetachEndpointsRequest{}, + }, + } + for _, testCase := range testCases { + result, _ := ToNetworkEndpointGroupsDetachEndpointsRequest(testCase.input) + if !reflect.DeepEqual(result, testCase.expected) { + t.Fatalf("ToNetworkEndpointGroupsDetachEndpointsRequest(input) = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(testCase.input), pretty.Sprint(result), pretty.Sprint(testCase.expected)) + } + } +} + +func TestNetworkEndpointGroupsDetachEndpointsRequestToAlpha(t *testing.T) { + composite := NetworkEndpointGroupsDetachEndpointsRequest{} + expected := &computealpha.NetworkEndpointGroupsDetachEndpointsRequest{} + result, err := composite.ToAlpha() + if err != nil { + t.Fatalf("NetworkEndpointGroupsDetachEndpointsRequest.ToAlpha() error: %v", err) + } + + if !reflect.DeepEqual(result, expected) { + t.Fatalf("NetworkEndpointGroupsDetachEndpointsRequest.ToAlpha() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected)) + } +} +func TestNetworkEndpointGroupsDetachEndpointsRequestToBeta(t *testing.T) { + composite := NetworkEndpointGroupsDetachEndpointsRequest{} + expected := &computebeta.NetworkEndpointGroupsDetachEndpointsRequest{} + result, err := composite.ToBeta() + if err != nil { + t.Fatalf("NetworkEndpointGroupsDetachEndpointsRequest.ToBeta() error: %v", err) + } + + if !reflect.DeepEqual(result, expected) { + t.Fatalf("NetworkEndpointGroupsDetachEndpointsRequest.ToBeta() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected)) + } +} +func TestNetworkEndpointGroupsDetachEndpointsRequestToGA(t *testing.T) { + composite := NetworkEndpointGroupsDetachEndpointsRequest{} + expected := &compute.NetworkEndpointGroupsDetachEndpointsRequest{} + result, err := composite.ToGA() + if err != nil { + t.Fatalf("NetworkEndpointGroupsDetachEndpointsRequest.ToGA() error: %v", err) + } + + if !reflect.DeepEqual(result, expected) { + t.Fatalf("NetworkEndpointGroupsDetachEndpointsRequest.ToGA() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected)) + } +} + func TestOriginAuthenticationMethod(t *testing.T) { compositeType := reflect.TypeOf(OriginAuthenticationMethod{}) alphaType := reflect.TypeOf(computealpha.OriginAuthenticationMethod{}) diff --git a/pkg/composite/meta/meta.go b/pkg/composite/meta/meta.go index c1c0208db2..ec63d69f63 100644 --- a/pkg/composite/meta/meta.go +++ b/pkg/composite/meta/meta.go @@ -36,13 +36,16 @@ const ( // The other types that are discovered as dependencies will simply be wrapped with a composite struct // The format of the map is ServiceName -> k8s-cloud-provider wrapper name var MainServices = map[string]string{ - "BackendService": "BackendServices", - "ForwardingRule": "ForwardingRules", - "HealthCheck": "HealthChecks", - "UrlMap": "UrlMaps", - "TargetHttpProxy": "TargetHttpProxies", - "TargetHttpsProxy": "TargetHttpsProxies", - "SslCertificate": "SslCertificates", + "BackendService": "BackendServices", + "ForwardingRule": "ForwardingRules", + "HealthCheck": "HealthChecks", + "UrlMap": "UrlMaps", + "TargetHttpProxy": "TargetHttpProxies", + "TargetHttpsProxy": "TargetHttpsProxies", + "SslCertificate": "SslCertificates", + "NetworkEndpointGroup": "NetworkEndpointGroups", + "NetworkEndpointGroupsAttachEndpointsRequest": "NetworkEndpointGroupsAttachEndpointsRequests", + "NetworkEndpointGroupsDetachEndpointsRequest": "NetworkEndpointGroupsDetachEndpointsRequests", } // TODO: (shance) Replace this with data gathered from meta.AllServices @@ -52,8 +55,14 @@ var NoUpdate = sets.NewString( "TargetHttpProxy", "TargetHttpsProxy", "SslCertificate", + "NetworkEndpointGroup", ) +// Services in NoCRUD will not have Create, Get, Delete, Update, methods generated for them +var NoCRUD = sets.NewString( + "NetworkEndpointGroupsAttachEndpointsRequest", + "NetworkEndpointGroupsDetachEndpointsRequest", +) var Versions = map[string]string{ "Alpha": "alpha", "Beta": "beta", @@ -67,6 +76,25 @@ var DefaultRegionalServices = sets.NewString( "ForwardingRule", ) +var DefaultZonalServices = sets.NewString( + "NetworkEndpointGroup", +) + +type GroupResourceInfo struct { + AttachFuncName string + DetachFuncName string + AttachReqName string + DetachReqName string +} + +// GroupResourceServices support adding/removing objects from them. Examples are Instance Groups, NetworkEndpointGroups. +// Additional APIs to Attach/Detach objects will be created for these services. +var GroupResourceServices = map[string]*GroupResourceInfo{ + "NetworkEndpointGroup": &GroupResourceInfo{AttachFuncName: "AttachNetworkEndpoints", + DetachFuncName: "DetachNetworkEndpoints", AttachReqName: "NetworkEndpointGroupsAttachEndpointsRequest", + DetachReqName: "NetworkEndpointGroupsDetachEndpointsRequest"}, +} + // ApiService holds relevant data for generating a composite type + helper methods for a single API service type ApiService struct { // Name of the Go struct @@ -96,10 +124,31 @@ func (apiService *ApiService) HasUpdate() bool { return !NoUpdate.Has(apiService.Name) } +// HasCRUD() returns true if the service name is *not* in the NoCRUD() list +func (apiService *ApiService) HasCRUD() bool { + return !NoCRUD.Has(apiService.Name) +} + func (apiService *ApiService) IsDefaultRegionalService() bool { return DefaultRegionalServices.Has(apiService.Name) } +func (apiService *ApiService) IsDefaultZonalService() bool { + return DefaultZonalServices.Has(apiService.Name) +} + +func (apiService *ApiService) IsGroupResourceService() bool { + _, ok := GroupResourceServices[apiService.Name] + return ok +} + +func (apiService *ApiService) GetGroupResourceInfo() *GroupResourceInfo { + if !(apiService.IsGroupResourceService()) { + return nil + } + return GroupResourceServices[apiService.Name] +} + // GetCloudProviderName() returns the name of the cloudprovider type for a service func (apiService *ApiService) GetCloudProviderName() string { result, ok := MainServices[apiService.Name]