github.com/cloudwan/edgelq-sdk@v1.15.4/iam/resources/v1alpha2/condition/condition.pb.parentname.go (about)

     1  // Code generated by protoc-gen-goten-resource
     2  // Resource: Condition
     3  // DO NOT EDIT!!!
     4  
     5  package condition
     6  
     7  import (
     8  	"fmt"
     9  	"net/url"
    10  	"reflect"
    11  	"regexp"
    12  	"strings"
    13  
    14  	"google.golang.org/grpc/codes"
    15  	"google.golang.org/grpc/status"
    16  	"google.golang.org/protobuf/proto"
    17  
    18  	"github.com/cloudwan/goten-sdk/runtime/goten"
    19  	gotenresource "github.com/cloudwan/goten-sdk/runtime/resource"
    20  )
    21  
    22  // proto imports
    23  import (
    24  	organization "github.com/cloudwan/edgelq-sdk/iam/resources/v1alpha2/organization"
    25  	project "github.com/cloudwan/edgelq-sdk/iam/resources/v1alpha2/project"
    26  	meta "github.com/cloudwan/goten-sdk/types/meta"
    27  )
    28  
    29  // ensure the imports are used
    30  var (
    31  	_ = codes.NotFound
    32  	_ = new(fmt.Stringer)
    33  	_ = new(proto.Message)
    34  	_ = status.Status{}
    35  	_ = url.URL{}
    36  	_ = strings.Builder{}
    37  
    38  	_ = new(goten.GotenMessage)
    39  	_ = new(gotenresource.ListQuery)
    40  )
    41  
    42  // make sure we're using proto imports
    43  var (
    44  	_ = &organization.Organization{}
    45  	_ = &project.Project{}
    46  	_ = &meta.Meta{}
    47  )
    48  
    49  var parentRegexPath = regexp.MustCompile("^$")
    50  var parentRegexPath_Project = regexp.MustCompile("^projects/(?P<project_id>-|[\\w][\\w.-]{0,127})$")
    51  var parentRegexPath_Organization = regexp.MustCompile("^organizations/(?P<organization_id>-|[\\w][\\w.-]{0,127})$")
    52  
    53  type ParentName struct {
    54  	NamePattern
    55  	ProjectId      string `firestore:"projectId"`
    56  	OrganizationId string `firestore:"organizationId"`
    57  }
    58  
    59  func ParseParentName(name string) (*ParentName, error) {
    60  	var matches []string
    61  	if matches = parentRegexPath.FindStringSubmatch(name); matches != nil {
    62  		return NewNameBuilder().
    63  			Parent(), nil
    64  	}
    65  	if matches = parentRegexPath_Project.FindStringSubmatch(name); matches != nil {
    66  		return NewNameBuilder().
    67  			SetProjectId(matches[1]).
    68  			Parent(), nil
    69  	}
    70  	if matches = parentRegexPath_Organization.FindStringSubmatch(name); matches != nil {
    71  		return NewNameBuilder().
    72  			SetOrganizationId(matches[1]).
    73  			Parent(), nil
    74  	}
    75  
    76  	return nil, status.Errorf(codes.InvalidArgument, "unable to parse '%s' as Condition parent name", name)
    77  }
    78  
    79  func MustParseParentName(name string) *ParentName {
    80  	result, err := ParseParentName(name)
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  	return result
    85  }
    86  
    87  func (name *ParentName) SetFromSegments(segments gotenresource.NameSegments) error {
    88  	if len(segments) == 0 {
    89  		name.Pattern = NamePattern_Nil
    90  		return nil
    91  	} else if len(segments) == 1 && segments[0].CollectionLowerJson == "projects" {
    92  		name.Pattern = NamePattern_Project
    93  		name.ProjectId = segments[0].Id
    94  		return nil
    95  	} else if len(segments) == 1 && segments[0].CollectionLowerJson == "organizations" {
    96  		name.Pattern = NamePattern_Organization
    97  		name.OrganizationId = segments[0].Id
    98  		return nil
    99  	}
   100  	return status.Errorf(codes.InvalidArgument, "unable to use segments %s to form Condition parent name", segments)
   101  }
   102  
   103  func (name *ParentName) GetProjectName() *project.Name {
   104  	if name == nil {
   105  		return nil
   106  	}
   107  
   108  	switch name.Pattern {
   109  	case NamePattern_Project:
   110  		return project.NewNameBuilder().
   111  			SetId(name.ProjectId).
   112  			Name()
   113  	default:
   114  		return nil
   115  	}
   116  }
   117  
   118  func (name *ParentName) GetOrganizationName() *organization.Name {
   119  	if name == nil {
   120  		return nil
   121  	}
   122  
   123  	switch name.Pattern {
   124  	case NamePattern_Organization:
   125  		return organization.NewNameBuilder().
   126  			SetId(name.OrganizationId).
   127  			Name()
   128  	default:
   129  		return nil
   130  	}
   131  }
   132  
   133  func (name *ParentName) IsSpecified() bool {
   134  	if name == nil || name.Pattern == "" {
   135  		return false
   136  	}
   137  	switch name.Pattern {
   138  	case NamePattern_Nil:
   139  		return true
   140  	case NamePattern_Project:
   141  		return name.ProjectId != ""
   142  	case NamePattern_Organization:
   143  		return name.OrganizationId != ""
   144  	}
   145  	return false
   146  }
   147  
   148  func (name *ParentName) IsFullyQualified() bool {
   149  	if name == nil || name.Pattern == "" {
   150  		return false
   151  	}
   152  
   153  	switch name.Pattern {
   154  	case NamePattern_Nil:
   155  		return true
   156  	case NamePattern_Project:
   157  		return name.ProjectId != "" && name.ProjectId != gotenresource.WildcardId
   158  	case NamePattern_Organization:
   159  		return name.OrganizationId != "" && name.OrganizationId != gotenresource.WildcardId
   160  	}
   161  
   162  	return false
   163  }
   164  
   165  func (name *ParentName) FullyQualifiedName() (string, error) {
   166  	if !name.IsFullyQualified() {
   167  		return "", status.Errorf(codes.InvalidArgument, "Parent name for Condition is not fully qualified")
   168  	}
   169  	return fmt.Sprintf("//iam.edgelq.com/%s", name.String()), nil
   170  }
   171  
   172  func (name *ParentName) GetResourceDescriptor() gotenresource.Descriptor {
   173  	return descriptor
   174  }
   175  
   176  func (name *ParentName) GetPattern() gotenresource.NamePattern {
   177  	if name == nil {
   178  		return ""
   179  	}
   180  	return name.Pattern
   181  }
   182  
   183  func (name *ParentName) GetIdParts() map[string]string {
   184  	if name != nil {
   185  		return map[string]string{
   186  			"projectId":      name.ProjectId,
   187  			"organizationId": name.OrganizationId,
   188  		}
   189  	}
   190  	return map[string]string{
   191  		"projectId":      "",
   192  		"organizationId": "",
   193  	}
   194  }
   195  
   196  func (name *ParentName) GetSegments() gotenresource.NameSegments {
   197  	if name == nil {
   198  		return nil
   199  	}
   200  
   201  	switch name.Pattern {
   202  	case NamePattern_Nil:
   203  		return gotenresource.NameSegments{}
   204  	case NamePattern_Project:
   205  		return gotenresource.NameSegments{
   206  			gotenresource.NameSegment{
   207  				CollectionLowerJson: "projects",
   208  				Id:                  name.ProjectId,
   209  			},
   210  		}
   211  	case NamePattern_Organization:
   212  		return gotenresource.NameSegments{
   213  			gotenresource.NameSegment{
   214  				CollectionLowerJson: "organizations",
   215  				Id:                  name.OrganizationId,
   216  			},
   217  		}
   218  	}
   219  	return nil
   220  }
   221  
   222  func (name *ParentName) GetIParentName() gotenresource.Name {
   223  	return nil
   224  }
   225  
   226  func (name *ParentName) GetIUnderlyingParentName() gotenresource.Name {
   227  	return nil
   228  }
   229  
   230  func (name *ParentName) String() string {
   231  	if name == nil {
   232  		return "<nil>"
   233  	}
   234  
   235  	if valueStr, err := name.ProtoString(); err != nil {
   236  		panic(err)
   237  	} else {
   238  		return valueStr
   239  	}
   240  }
   241  
   242  func (name *ParentName) DescendsFrom(ancestor string) bool {
   243  	if name == nil {
   244  		return false
   245  	}
   246  
   247  	switch name.Pattern {
   248  	case NamePattern_Project:
   249  		return ancestor == "projects"
   250  	case NamePattern_Organization:
   251  		return ancestor == "organizations"
   252  	}
   253  
   254  	return false
   255  }
   256  
   257  func (name *ParentName) AsReference() *ParentReference {
   258  	return &ParentReference{ParentName: *name}
   259  }
   260  
   261  func (name *ParentName) AsRawReference() gotenresource.Reference {
   262  	return name.AsReference()
   263  }
   264  
   265  // implement methods required by protobuf-go library for string-struct conversion
   266  
   267  func (name *ParentName) ProtoString() (string, error) {
   268  	if name == nil {
   269  		return "", nil
   270  	}
   271  	switch name.Pattern {
   272  	case NamePattern_Project:
   273  		return "projects/" + name.ProjectId, nil
   274  	case NamePattern_Organization:
   275  		return "organizations/" + name.OrganizationId, nil
   276  	}
   277  	return "", nil
   278  }
   279  
   280  func (name *ParentName) ParseProtoString(data string) error {
   281  	parsed, err := ParseParentName(data)
   282  	if err != nil {
   283  		return err
   284  	}
   285  	*name = *parsed
   286  	return nil
   287  }
   288  
   289  // GotenEqual returns true if other is of same type and paths are equal (implements goten.Equaler interface)
   290  func (name *ParentName) GotenEqual(other interface{}) bool {
   291  	if other == nil {
   292  		return name == nil
   293  	}
   294  	other1, ok := other.(*ParentName)
   295  	if !ok {
   296  		other2, ok := other.(ParentName)
   297  		if ok {
   298  			other1 = &other2
   299  		} else {
   300  			return false
   301  		}
   302  	}
   303  	if other1 == nil {
   304  		return name == nil
   305  	} else if name == nil {
   306  		return false
   307  	}
   308  	if name.ProjectId != other1.ProjectId {
   309  		return false
   310  	}
   311  	if name.OrganizationId != other1.OrganizationId {
   312  		return false
   313  	}
   314  	if name.Pattern != other1.Pattern {
   315  		return false
   316  	}
   317  
   318  	return true
   319  }
   320  
   321  // Matches is same as GotenEqual, but also will accept "other" if name is wildcard.
   322  func (name *ParentName) Matches(other interface{}) bool {
   323  	if other == nil {
   324  		return name == nil
   325  	}
   326  	other1, ok := other.(*ParentName)
   327  	if !ok {
   328  		other2, ok := other.(ParentName)
   329  		if ok {
   330  			other1 = &other2
   331  		} else {
   332  			return false
   333  		}
   334  	}
   335  	if other1 == nil {
   336  		return name == nil
   337  	} else if name == nil {
   338  		return false
   339  	}
   340  
   341  	if name.Pattern != other1.Pattern {
   342  		return false
   343  	}
   344  	switch name.Pattern {
   345  	case NamePattern_Project:
   346  		if name.ProjectId != other1.ProjectId &&
   347  			name.ProjectId != gotenresource.WildcardId {
   348  			return false
   349  		}
   350  	case NamePattern_Organization:
   351  		if name.OrganizationId != other1.OrganizationId &&
   352  			name.OrganizationId != gotenresource.WildcardId {
   353  			return false
   354  		}
   355  	}
   356  
   357  	return true
   358  }
   359  
   360  // implement CustomTypeCliValue method
   361  func (name *ParentName) SetFromCliFlag(raw string) error {
   362  	parsedName, err := ParseParentName(raw)
   363  	if err != nil {
   364  		return err
   365  	}
   366  	*name = *parsedName
   367  	return nil
   368  }
   369  
   370  type ParentReference struct {
   371  	ParentName
   372  	project      *project.Project
   373  	organization *organization.Organization
   374  }
   375  
   376  func MakeParentReference(name *ParentName) (*ParentReference, error) {
   377  	return &ParentReference{
   378  		ParentName: *name,
   379  	}, nil
   380  }
   381  
   382  func ParseParentReference(name string) (*ParentReference, error) {
   383  	parsedName, err := ParseParentName(name)
   384  	if err != nil {
   385  		return nil, err
   386  	}
   387  	return MakeParentReference(parsedName)
   388  }
   389  
   390  func MustParseParentReference(name string) *ParentReference {
   391  	result, err := ParseParentReference(name)
   392  	if err != nil {
   393  		panic(err)
   394  	}
   395  	return result
   396  }
   397  func (ref *ParentReference) GetProjectReference() *project.Reference {
   398  	if ref == nil {
   399  		return nil
   400  	}
   401  
   402  	switch ref.Pattern {
   403  	case NamePattern_Project:
   404  		return project.NewNameBuilder().
   405  			SetId(ref.ProjectId).
   406  			Reference()
   407  	default:
   408  		return nil
   409  	}
   410  }
   411  func (ref *ParentReference) GetOrganizationReference() *organization.Reference {
   412  	if ref == nil {
   413  		return nil
   414  	}
   415  
   416  	switch ref.Pattern {
   417  	case NamePattern_Organization:
   418  		return organization.NewNameBuilder().
   419  			SetId(ref.OrganizationId).
   420  			Reference()
   421  	default:
   422  		return nil
   423  	}
   424  }
   425  
   426  func (ref *ParentReference) GetUnderlyingReference() gotenresource.Reference {
   427  	if ref == nil {
   428  		return nil
   429  	}
   430  	projectRef := ref.GetProjectReference()
   431  	if projectRef != nil {
   432  		return projectRef
   433  	}
   434  	organizationRef := ref.GetOrganizationReference()
   435  	if organizationRef != nil {
   436  		return organizationRef
   437  	}
   438  
   439  	return nil
   440  }
   441  
   442  func (ref *ParentReference) ResolveRaw(res gotenresource.Resource) error {
   443  	switch typedRes := res.(type) {
   444  	case *project.Project:
   445  		if name := ref.GetProjectName(); name == nil {
   446  			return status.Errorf(codes.InvalidArgument, "cannot set Project as parent of Condition, because pattern does not match")
   447  		}
   448  		ref.project = typedRes
   449  		return nil
   450  	case *organization.Organization:
   451  		if name := ref.GetOrganizationName(); name == nil {
   452  			return status.Errorf(codes.InvalidArgument, "cannot set Organization as parent of Condition, because pattern does not match")
   453  		}
   454  		ref.organization = typedRes
   455  		return nil
   456  	default:
   457  		return status.Errorf(codes.Internal, "Invalid parent type for Condition, got %s", reflect.TypeOf(res).Elem().Name())
   458  	}
   459  }
   460  
   461  func (ref *ParentReference) Resolved() bool {
   462  	if name := ref.GetProjectName(); name != nil {
   463  		return ref.project != nil
   464  	}
   465  	if name := ref.GetOrganizationName(); name != nil {
   466  		return ref.organization != nil
   467  	}
   468  	return true
   469  }
   470  
   471  func (ref *ParentReference) ClearCached() {
   472  	ref.project = nil
   473  	ref.organization = nil
   474  }
   475  
   476  func (ref *ParentReference) GetProject() *project.Project {
   477  	if ref == nil {
   478  		return nil
   479  	}
   480  	return ref.project
   481  }
   482  func (ref *ParentReference) GetOrganization() *organization.Organization {
   483  	if ref == nil {
   484  		return nil
   485  	}
   486  	return ref.organization
   487  }
   488  
   489  func (ref *ParentReference) GetRawResource() gotenresource.Resource {
   490  	if name := ref.ParentName.GetProjectName(); name != nil {
   491  		return ref.project
   492  	}
   493  	if name := ref.ParentName.GetOrganizationName(); name != nil {
   494  		return ref.organization
   495  	}
   496  	return nil
   497  }
   498  
   499  func (ref *ParentReference) IsFullyQualified() bool {
   500  	if ref == nil {
   501  		return false
   502  	}
   503  	return ref.ParentName.IsFullyQualified()
   504  }
   505  
   506  func (ref *ParentReference) IsSpecified() bool {
   507  	if ref == nil {
   508  		return false
   509  	}
   510  	return ref.ParentName.IsSpecified()
   511  }
   512  
   513  func (ref *ParentReference) GetResourceDescriptor() gotenresource.Descriptor {
   514  	return descriptor
   515  }
   516  
   517  func (ref *ParentReference) GetPattern() gotenresource.NamePattern {
   518  	if ref == nil {
   519  		return ""
   520  	}
   521  	return ref.Pattern
   522  }
   523  
   524  func (ref *ParentReference) GetIdParts() map[string]string {
   525  	if ref != nil {
   526  		return ref.ParentName.GetIdParts()
   527  	}
   528  	return map[string]string{
   529  		"projectId":      "",
   530  		"organizationId": "",
   531  	}
   532  }
   533  
   534  func (ref *ParentReference) GetSegments() gotenresource.NameSegments {
   535  	if ref != nil {
   536  		return ref.ParentName.GetSegments()
   537  	}
   538  	return nil
   539  }
   540  
   541  func (ref *ParentReference) GetIParentName() gotenresource.Name {
   542  	return nil
   543  }
   544  
   545  func (ref *ParentReference) GetIUnderlyingParentName() gotenresource.Name {
   546  	return nil
   547  }
   548  
   549  func (ref *ParentReference) String() string {
   550  	if ref == nil {
   551  		return "<nil>"
   552  	}
   553  	return ref.ParentName.String()
   554  }
   555  
   556  // implement methods required by protobuf-go library for string-struct conversion
   557  
   558  func (ref *ParentReference) ProtoString() (string, error) {
   559  	if ref == nil {
   560  		return "", nil
   561  	}
   562  	return ref.ParentName.ProtoString()
   563  }
   564  
   565  func (ref *ParentReference) ParseProtoString(data string) error {
   566  	parsed, err := ParseParentReference(data)
   567  	if err != nil {
   568  		return err
   569  	}
   570  	*ref = *parsed
   571  	return nil
   572  }
   573  
   574  // GotenEqual returns true if other is of same type and paths are equal (implements goten.Equaler interface)
   575  func (ref *ParentReference) GotenEqual(other interface{}) bool {
   576  	if other == nil {
   577  		return ref == nil
   578  	}
   579  	other1, ok := other.(*ParentReference)
   580  	if !ok {
   581  		other2, ok := other.(ParentReference)
   582  		if ok {
   583  			other1 = &other2
   584  		} else {
   585  			return false
   586  		}
   587  	}
   588  	if other1 == nil {
   589  		return ref == nil
   590  	} else if ref == nil {
   591  		return false
   592  	}
   593  	if ref.project != other1.project {
   594  		return false
   595  	}
   596  	if ref.organization != other1.organization {
   597  		return false
   598  	}
   599  
   600  	return ref.ParentName.GotenEqual(other1.ParentName)
   601  }
   602  
   603  // Matches is same as GotenEqual, but also will accept "other" if name is wildcard.
   604  func (name *ParentReference) Matches(other interface{}) bool {
   605  	if other == nil {
   606  		return name == nil
   607  	}
   608  	other1, ok := other.(*ParentReference)
   609  	if !ok {
   610  		other2, ok := other.(ParentReference)
   611  		if ok {
   612  			other1 = &other2
   613  		} else {
   614  			return false
   615  		}
   616  	}
   617  	if other1 == nil {
   618  		return name == nil
   619  	} else if name == nil {
   620  		return false
   621  	}
   622  	return name.ParentName.Matches(&other1.ParentName)
   623  }
   624  
   625  // implement CustomTypeCliValue method
   626  func (ref *ParentReference) SetFromCliFlag(raw string) error {
   627  	parsedRef, err := ParseParentReference(raw)
   628  	if err != nil {
   629  		return err
   630  	}
   631  	*ref = *parsedRef
   632  	return nil
   633  }