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

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