All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.cloud.compute.ServiceAccount Maven / Gradle / Ivy

/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * 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
 *
 *       http://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.
 */

package com.google.cloud.compute;

import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * A service account, with its specified scopes, authorized for this instance.
 *
 * @see Authenticating from Google
 *     Compute Engine
 */
public final class ServiceAccount implements Serializable {

  static final Function
      FROM_PB_FUNCTION =
      new Function() {
        @Override
        public ServiceAccount apply(com.google.api.services.compute.model.ServiceAccount pb) {
          return ServiceAccount.fromPb(pb);
        }
      };
  static final Function
      TO_PB_FUNCTION =
      new Function() {
        @Override
        public com.google.api.services.compute.model.ServiceAccount apply(
            ServiceAccount metadata) {
          return metadata.toPb();
        }
      };

  private static final long serialVersionUID = 4199610694227857331L;

  private final String email;
  private final List scopes;

  private ServiceAccount(String email, List scopes) {
    this.email = email;
    this.scopes = ImmutableList.copyOf(scopes);
  }

  /**
   * Returns the email address of the service account.
   */
  public String email() {
    return email;
  }

  /**
   * Returns the list of scopes to be made available for this service account.
   */
  public List scopes() {
    return scopes;
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this)
        .add("email", email)
        .add("scopes", scopes)
        .toString();
  }

  @Override
  public int hashCode() {
    return Objects.hash(email, scopes);
  }

  @Override
  public boolean equals(Object obj) {
    return obj == this
        || obj instanceof ServiceAccount
        && Objects.equals(toPb(), ((ServiceAccount) obj).toPb());
  }

  com.google.api.services.compute.model.ServiceAccount toPb() {
    com.google.api.services.compute.model.ServiceAccount serviceAccountPb =
        new com.google.api.services.compute.model.ServiceAccount();
    serviceAccountPb.setEmail(email);
    serviceAccountPb.setScopes(scopes);
    return serviceAccountPb;
  }

  /**
   * Returns a {@code ServiceAccount} object for the provided email and scopes.
   */
  public static ServiceAccount of(String email, List scopes) {
    return new ServiceAccount(email, scopes);
  }

  /**
   * Returns a {@code ServiceAccount} object for the provided email and scopes.
   */
  public static ServiceAccount of(String email, String... scopes) {
    return of(email, Arrays.asList(scopes));
  }

  static ServiceAccount fromPb(com.google.api.services.compute.model.ServiceAccount accountPb) {
    return new ServiceAccount(accountPb.getEmail(), accountPb.getScopes());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy