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