Package org.jclouds.googlecomputeengine.compute.strategy

Source Code of org.jclouds.googlecomputeengine.compute.strategy.UseNodeCredentialsButOverrideFromTemplate

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.jclouds.googlecomputeengine.compute.strategy;

import com.google.common.base.Function;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.compute.strategy.PrioritizeCredentialsFromTemplate;
import org.jclouds.domain.LoginCredentials;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* GCE needs the credentials to create the node so the node credentials already take the Image credentials into account,
* as such only overriding the TemplateOptions credentials is required.
*
* @author David Alves
*/
@Singleton
public class UseNodeCredentialsButOverrideFromTemplate extends PrioritizeCredentialsFromTemplate {


   @Inject
   public UseNodeCredentialsButOverrideFromTemplate(
           Function<Template, LoginCredentials> credentialsFromImageOrTemplateOptions) {
      super(credentialsFromImageOrTemplateOptions);
   }

   public LoginCredentials apply(Template template, LoginCredentials fromNode) {
      RunScriptOptions options = checkNotNull(template.getOptions(), "template options are required");
      LoginCredentials.Builder builder = LoginCredentials.builder(fromNode);
      if (options.getLoginUser() != null)
         builder.user(template.getOptions().getLoginUser());
      if (options.getLoginPassword() != null)
         builder.password(options.getLoginPassword());
      if (options.getLoginPrivateKey() != null)
         builder.privateKey(options.getLoginPrivateKey());
      if (options.shouldAuthenticateSudo() != null && options.shouldAuthenticateSudo())
         builder.authenticateSudo(true);
      return builder.build();
   }
}
TOP

Related Classes of org.jclouds.googlecomputeengine.compute.strategy.UseNodeCredentialsButOverrideFromTemplate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.