findPolicyMessage finds the policy and the parent name by the policy name.
(ctx context.Context, policyName string)
| 347 | |
| 348 | // findPolicyMessage finds the policy and the parent name by the policy name. |
| 349 | func (s *OrgPolicyService) findPolicyMessage(ctx context.Context, policyName string) (*store.PolicyMessage, string, error) { |
| 350 | tokens := strings.Split(policyName, common.PolicyNamePrefix) |
| 351 | if len(tokens) != 2 { |
| 352 | return nil, "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid request %s", policyName)) |
| 353 | } |
| 354 | |
| 355 | policyParent := tokens[0] |
| 356 | if strings.HasSuffix(policyParent, "/") { |
| 357 | policyParent = policyParent[:(len(policyParent) - 1)] |
| 358 | } |
| 359 | resourceType, resource, err := common.GetPolicyResourceTypeAndResource(policyParent) |
| 360 | if err != nil { |
| 361 | return nil, policyParent, connect.NewError(connect.CodeInvalidArgument, err) |
| 362 | } |
| 363 | if resource == nil { |
| 364 | return nil, policyParent, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("resource for %s must be specific", resourceType)) |
| 365 | } |
| 366 | |
| 367 | // Parse the policy type from the string in the policy name |
| 368 | v1PolicyType, ok := v1pb.PolicyType_value[strings.ToUpper(tokens[1])] |
| 369 | if !ok { |
| 370 | return nil, policyParent, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid policy type %v", tokens[1])) |
| 371 | } |
| 372 | |
| 373 | policyType, err := convertV1PBToStorePBPolicyType(v1pb.PolicyType(v1PolicyType)) |
| 374 | if err != nil { |
| 375 | return nil, policyParent, connect.NewError(connect.CodeInvalidArgument, err) |
| 376 | } |
| 377 | |
| 378 | policy, err := s.store.GetPolicy(ctx, &store.FindPolicyMessage{ |
| 379 | Workspace: common.GetWorkspaceIDFromContext(ctx), |
| 380 | ResourceType: &resourceType, |
| 381 | Type: &policyType, |
| 382 | Resource: resource, |
| 383 | }) |
| 384 | if err != nil { |
| 385 | return nil, policyParent, connect.NewError(connect.CodeInternal, err) |
| 386 | } |
| 387 | if policy == nil { |
| 388 | return nil, policyParent, connect.NewError(connect.CodeNotFound, errors.Errorf("policy %q not found", policyName)) |
| 389 | } |
| 390 | |
| 391 | return policy, policyParent, nil |
| 392 | } |
| 393 | |
| 394 | func (s *OrgPolicyService) createPolicyMessage(ctx context.Context, req *connect.Request[v1pb.CreatePolicyRequest]) (*v1pb.Policy, error) { |
| 395 | parent := req.Msg.Parent |
no test coverage detected