// Code generated by sidekick. DO NOT EDIT.
//
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import GoogleCloudWkt

/// An [admission rule][google.cloud.binaryauthorization.v1.AdmissionRule]
/// specifies either that all container images used in a pod creation request
/// must be attested to by one or more
/// [attestors][google.cloud.binaryauthorization.v1.Attestor], that all pod
/// creations will be allowed, or that all pod creations will be denied.
///
/// Images matching an [admission allowlist
/// pattern][google.cloud.binaryauthorization.v1.AdmissionWhitelistPattern] are
/// exempted from admission rules and will never block a pod creation.
///
/// [google.cloud.binaryauthorization.v1.AdmissionRule]: <doc:AdmissionRule>
/// [google.cloud.binaryauthorization.v1.AdmissionWhitelistPattern]: <doc:AdmissionWhitelistPattern>
/// [google.cloud.binaryauthorization.v1.Attestor]: <doc:Attestor>
public struct AdmissionRule: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// Required. How this admission rule will be evaluated.
  public var evaluationMode: AdmissionRule.EvaluationMode = AdmissionRule.EvaluationMode()

  /// Optional. The resource names of the attestors that must attest to
  /// a container image, in the format `projects/*/attestors/*`. Each
  /// attestor must exist before a policy can reference it.  To add an attestor
  /// to a policy the principal issuing the policy change request must be able
  /// to read the attestor resource.
  ///
  /// Note: this field must be non-empty when the `evaluation_mode` field
  /// specifies `REQUIRE_ATTESTATION`, otherwise it must be empty.
  public var requireAttestationsBy: [Swift.String] = []

  /// Required. The action when a pod creation is denied by the admission rule.
  public var enforcementMode: AdmissionRule.EnforcementMode = AdmissionRule.EnforcementMode()

  /// Initialize a new instance of `AdmissionRule`.
  public init() {}

  /// Use `config` to return a new instance of this object, with some fields updated.
  ///
  /// Commonly used to initialize the value, for example:
  ///
  /// ```
  /// let value = AdmissionRule().with { $0.evaluationMode = ... }
  /// ```
  public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
    var copy = self
    try config(&copy)
    return copy
  }

  public enum EvaluationMode: Codable, Equatable, Sendable {
    /// Do not use.
    case unspecified
    /// This rule allows all pod creations.
    case alwaysAllow
    /// This rule allows a pod creation if all the attestors listed in
    /// `require_attestations_by` have valid attestations for all of the
    /// images in the pod spec.
    case requireAttestation
    /// This rule denies all pod creations.
    case alwaysDeny
    /// Encodes an unknown integer value.
    ///
    /// The most common cause for an unknown values is for the service to send
    /// a value unknown to the library. We recommend you update your library to
    /// the latest version.
    case unknownIntValue(Int)
    /// Encodes an unknown string value.
    ///
    /// The most common cause for an unknown values is for the service to send
    /// a value unknown to the library. We recommend you update your library to
    /// the latest version.
    case unknownStringValue(String)

    public init() {
      self = .unspecified
    }

    /// Returns the integer value associated with the enumeration.
    ///
    /// If the enumeration was initialized with an unknown string value, this returns `nil`.
    public var intValue: Int? {
      switch self {
      case .unspecified: return 0
      case .alwaysAllow: return 1
      case .requireAttestation: return 2
      case .alwaysDeny: return 3
      case .unknownIntValue(let v): return v
      case .unknownStringValue: return nil
      }
    }

    /// Returns the string value (or name) associated with the enumeration.
    ///
    /// If the enumeration was initialized with an unknown integer value, this returns `nil`.
    public var stringValue: Swift.String? {
      switch self {
      case .unspecified: return "EVALUATION_MODE_UNSPECIFIED"
      case .alwaysAllow: return "ALWAYS_ALLOW"
      case .requireAttestation: return "REQUIRE_ATTESTATION"
      case .alwaysDeny: return "ALWAYS_DENY"
      case .unknownIntValue: return nil
      case .unknownStringValue(let v): return v
      }
    }

    /// Initialize from a string value.
    ///
    /// If the value is unknown, this initializes to [`unknownStringValue`](doc:EvaluationMode/unknownStringValue(_:)).
    public init(stringValue: Swift.String) {
      switch stringValue {
      case "EVALUATION_MODE_UNSPECIFIED": self = .unspecified
      case "ALWAYS_ALLOW": self = .alwaysAllow
      case "REQUIRE_ATTESTATION": self = .requireAttestation
      case "ALWAYS_DENY": self = .alwaysDeny
      default: self = .unknownStringValue(stringValue)
      }
    }

    /// Initialize from an integer value.
    ///
    /// If the value is unknown, this initializes to [`unknownIntValue`](doc:EvaluationMode/unknownIntValue(_:)).
    public init(intValue: Int) {
      switch intValue {
      case 0: self = .unspecified
      case 1: self = .alwaysAllow
      case 2: self = .requireAttestation
      case 3: self = .alwaysDeny
      default: self = .unknownIntValue(intValue)
      }
    }

    public init(from decoder: Decoder) throws {
      let container = try decoder.singleValueContainer()
      if let v = try? container.decode(Int.self) {
        self.init(intValue: v)
        return
      }
      if let s = try? container.decode(String.self) {
        if let v = Int(s) {
          self.init(intValue: v)
        } else {
          self.init(stringValue: s)
        }
        return
      }
      throw DecodingError.dataCorruptedError(
        in: container, debugDescription: "Expected enum value, must be integer or string.")
    }

    public func encode(to encoder: Encoder) throws {
      var container = encoder.singleValueContainer()
      switch self {
      case .unspecified: return try container.encode(0)
      case .alwaysAllow: return try container.encode(1)
      case .requireAttestation: return try container.encode(2)
      case .alwaysDeny: return try container.encode(3)
      case .unknownIntValue(let v): return try container.encode(v)
      case .unknownStringValue(let v): return try container.encode(v)
      }
    }
  }

  /// Defines the possible actions when a pod creation is denied by an admission
  /// rule.
  public enum EnforcementMode: Codable, Equatable, Sendable {
    /// Do not use.
    case unspecified
    /// Enforce the admission rule by blocking the pod creation.
    case enforcedBlockAndAuditLog
    /// Dryrun mode: Audit logging only.  This will allow the pod creation as if
    /// the admission request had specified break-glass.
    case dryrunAuditLogOnly
    /// Encodes an unknown integer value.
    ///
    /// The most common cause for an unknown values is for the service to send
    /// a value unknown to the library. We recommend you update your library to
    /// the latest version.
    case unknownIntValue(Int)
    /// Encodes an unknown string value.
    ///
    /// The most common cause for an unknown values is for the service to send
    /// a value unknown to the library. We recommend you update your library to
    /// the latest version.
    case unknownStringValue(String)

    public init() {
      self = .unspecified
    }

    /// Returns the integer value associated with the enumeration.
    ///
    /// If the enumeration was initialized with an unknown string value, this returns `nil`.
    public var intValue: Int? {
      switch self {
      case .unspecified: return 0
      case .enforcedBlockAndAuditLog: return 1
      case .dryrunAuditLogOnly: return 2
      case .unknownIntValue(let v): return v
      case .unknownStringValue: return nil
      }
    }

    /// Returns the string value (or name) associated with the enumeration.
    ///
    /// If the enumeration was initialized with an unknown integer value, this returns `nil`.
    public var stringValue: Swift.String? {
      switch self {
      case .unspecified: return "ENFORCEMENT_MODE_UNSPECIFIED"
      case .enforcedBlockAndAuditLog: return "ENFORCED_BLOCK_AND_AUDIT_LOG"
      case .dryrunAuditLogOnly: return "DRYRUN_AUDIT_LOG_ONLY"
      case .unknownIntValue: return nil
      case .unknownStringValue(let v): return v
      }
    }

    /// Initialize from a string value.
    ///
    /// If the value is unknown, this initializes to [`unknownStringValue`](doc:EnforcementMode/unknownStringValue(_:)).
    public init(stringValue: Swift.String) {
      switch stringValue {
      case "ENFORCEMENT_MODE_UNSPECIFIED": self = .unspecified
      case "ENFORCED_BLOCK_AND_AUDIT_LOG": self = .enforcedBlockAndAuditLog
      case "DRYRUN_AUDIT_LOG_ONLY": self = .dryrunAuditLogOnly
      default: self = .unknownStringValue(stringValue)
      }
    }

    /// Initialize from an integer value.
    ///
    /// If the value is unknown, this initializes to [`unknownIntValue`](doc:EnforcementMode/unknownIntValue(_:)).
    public init(intValue: Int) {
      switch intValue {
      case 0: self = .unspecified
      case 1: self = .enforcedBlockAndAuditLog
      case 2: self = .dryrunAuditLogOnly
      default: self = .unknownIntValue(intValue)
      }
    }

    public init(from decoder: Decoder) throws {
      let container = try decoder.singleValueContainer()
      if let v = try? container.decode(Int.self) {
        self.init(intValue: v)
        return
      }
      if let s = try? container.decode(String.self) {
        if let v = Int(s) {
          self.init(intValue: v)
        } else {
          self.init(stringValue: s)
        }
        return
      }
      throw DecodingError.dataCorruptedError(
        in: container, debugDescription: "Expected enum value, must be integer or string.")
    }

    public func encode(to encoder: Encoder) throws {
      var container = encoder.singleValueContainer()
      switch self {
      case .unspecified: return try container.encode(0)
      case .enforcedBlockAndAuditLog: return try container.encode(1)
      case .dryrunAuditLogOnly: return try container.encode(2)
      case .unknownIntValue(let v): return try container.encode(v)
      case .unknownStringValue(let v): return try container.encode(v)
      }
    }
  }

  public static var _anyTypeUrl: Swift.String {
    return "type.googleapis.com/google.cloud.binaryauthorization.v1.AdmissionRule"
  }
  public init(fromAny any: GoogleCloudWkt.`Any`) throws {
    self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
  }
  public func _pack() throws -> GoogleCloudWkt.Struct {
    return try GoogleCloudWkt._slowAnySerialize(message: self)
  }
}
