1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.compute.config; |
20 | |
21 | import java.util.Map; |
22 | |
23 | import org.jclouds.javax.annotation.Nullable; |
24 | |
25 | import org.jclouds.compute.domain.NodeMetadata; |
26 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
27 | import org.jclouds.compute.internal.PersistNodeCredentials; |
28 | import org.jclouds.domain.Credentials; |
29 | import org.jclouds.scriptbuilder.domain.Statement; |
30 | import org.jclouds.scriptbuilder.functions.CredentialsFromAdminAccess; |
31 | |
32 | import com.google.common.base.Function; |
33 | import com.google.inject.AbstractModule; |
34 | import com.google.inject.Inject; |
35 | import com.google.inject.TypeLiteral; |
36 | import com.google.inject.assistedinject.Assisted; |
37 | import com.google.inject.assistedinject.FactoryModuleBuilder; |
38 | import com.google.inject.name.Names; |
39 | |
40 | /** |
41 | * |
42 | * @author Adrian Cole |
43 | */ |
44 | public class PersistNodeCredentialsModule extends AbstractModule { |
45 | |
46 | static class RefreshCredentialsForNodeIfRanAdminAccess implements Function<NodeMetadata, NodeMetadata> { |
47 | protected final Map<String, Credentials> credentialStore; |
48 | protected final Statement statement; |
49 | |
50 | @Inject |
51 | protected RefreshCredentialsForNodeIfRanAdminAccess(Map<String, Credentials> credentialStore, |
52 | @Nullable @Assisted Statement statement) { |
53 | this.credentialStore = credentialStore; |
54 | this.statement = statement; |
55 | } |
56 | |
57 | @Override |
58 | public NodeMetadata apply(NodeMetadata input) { |
59 | if (statement == null) |
60 | return input; |
61 | Credentials credentials = CredentialsFromAdminAccess.INSTANCE.apply(statement); |
62 | if (credentials != null) { |
63 | input = NodeMetadataBuilder.fromNodeMetadata(input).credentials(credentials).build(); |
64 | credentialStore.put("node#" + input.getId(), input.getCredentials()); |
65 | } |
66 | return input; |
67 | } |
68 | |
69 | } |
70 | |
71 | static class RefreshCredentialsForNode extends RefreshCredentialsForNodeIfRanAdminAccess { |
72 | |
73 | @Inject |
74 | public RefreshCredentialsForNode(Map<String, Credentials> credentialStore, @Assisted @Nullable Statement statement) { |
75 | super(credentialStore, statement); |
76 | } |
77 | |
78 | @Override |
79 | public NodeMetadata apply(NodeMetadata input) { |
80 | input = super.apply(input); |
81 | if (input.getCredentials() != null) |
82 | credentialStore.put("node#" + input.getId(), input.getCredentials()); |
83 | return input; |
84 | } |
85 | |
86 | } |
87 | |
88 | @Override |
89 | protected void configure() { |
90 | |
91 | install(new FactoryModuleBuilder().implement(new TypeLiteral<Function<NodeMetadata, NodeMetadata>>() { |
92 | }, Names.named("ifAdminAccess"), RefreshCredentialsForNodeIfRanAdminAccess.class) |
93 | .implement(new TypeLiteral<Function<NodeMetadata, NodeMetadata>>() { |
94 | }, Names.named("always"), RefreshCredentialsForNode.class).build(PersistNodeCredentials.class)); |
95 | } |
96 | |
97 | } |