1package autotest.moblab;
2
3import autotest.common.JsonRpcCallback;
4import autotest.common.JsonRpcProxy;
5import autotest.common.ui.NotifyManager;
6import autotest.common.ui.TabView;
7import autotest.common.ui.ToolTip;
8import com.google.gwt.event.dom.client.ClickEvent;
9import com.google.gwt.event.dom.client.ClickHandler;
10import com.google.gwt.json.client.JSONObject;
11import com.google.gwt.json.client.JSONString;
12import com.google.gwt.json.client.JSONValue;
13import com.google.gwt.user.client.ui.Button;
14import com.google.gwt.user.client.ui.FileUpload;
15import com.google.gwt.user.client.ui.FormPanel;
16import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
17import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
18import java.util.HashMap;
19import java.util.Map;
20
21/**
22 * Widget to upload all moblab credentials.
23 */
24public class CredentialsUploadView extends TabView {
25  private static final String UPLOAD_KEY_BOTO_KEY = "boto_key";
26  private static final String UPLOAD_KEY_SERVICE_ACCOUNT = "service_account";
27  private static final String UPLOAD_KEY_LAUNCH_CONTROL_KEY = "launch_control_key";
28
29  static Map<String, UploadInfo> uploadMap = new HashMap<String, UploadInfo>();
30  static {
31    uploadMap.put(
32        UPLOAD_KEY_BOTO_KEY,
33        new UploadInfo(
34            UPLOAD_KEY_BOTO_KEY, "set_boto_key", "boto_key", "Boto Key uploaded.",
35            "Boto key is for legacy access to Google Storage."));
36    uploadMap.put(
37        UPLOAD_KEY_SERVICE_ACCOUNT,
38        new UploadInfo(
39            UPLOAD_KEY_SERVICE_ACCOUNT,
40            "set_service_account_credential",
41            "service_account_filename",
42            "Service Account Credential file uploaded.",
43            "Service Account Credential is for access to Google Cloud Platform resources, "
44            + "such as Google Storage, Cloud PubSub or others."));
45    uploadMap.put(
46        UPLOAD_KEY_LAUNCH_CONTROL_KEY,
47        new UploadInfo(
48            UPLOAD_KEY_LAUNCH_CONTROL_KEY,
49            "set_launch_control_key",
50            "launch_control_key",
51            "Launch Control Key uploaded.",
52            "Launch Control Key is for access to the android build."));
53  }
54
55  @Override
56  public String getElementId() {
57    return "moblab_credentials";
58  }
59
60  @Override
61  public void initialize() {
62    super.initialize();
63
64    addUploadWidget(uploadMap.get(UPLOAD_KEY_BOTO_KEY));
65    addUploadWidget(uploadMap.get(UPLOAD_KEY_SERVICE_ACCOUNT));
66    addUploadWidget(uploadMap.get(UPLOAD_KEY_LAUNCH_CONTROL_KEY));
67  }
68
69  /**
70   * Creates widget for an upload.
71   *
72   * @param uploadInfo the upload information.
73   */
74  private void addUploadWidget(final UploadInfo uploadInfo) {
75    final FormPanel keyUploadForm = new FormPanel();
76    keyUploadForm.setAction(JsonRpcProxy.AFE_BASE_URL + "upload/");
77    keyUploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
78    keyUploadForm.setMethod(FormPanel.METHOD_POST);
79
80    FileUpload keyUpload = new FileUpload();
81    keyUpload.setName(uploadInfo.fileUploadName);
82    keyUploadForm.setWidget(keyUpload);
83
84    Button submitButton =
85        new Button(
86            "Submit",
87            new ClickHandler() {
88              @Override
89              public void onClick(ClickEvent event) {
90                keyUploadForm.submit();
91              }
92            });
93
94    keyUploadForm.addSubmitCompleteHandler(
95        new SubmitCompleteHandler() {
96          @Override
97          public void onSubmitComplete(SubmitCompleteEvent event) {
98            String fileName = event.getResults();
99            JSONObject params = new JSONObject();
100            params.put(uploadInfo.rpcArgName, new JSONString(fileName));
101            rpcCall(uploadInfo.rpcName, params, uploadInfo.successMessage);
102          }
103        });
104
105    addWidget(keyUploadForm, uploadInfo.uploadViewId);
106    addWidget(submitButton, uploadInfo.submitButtonId);
107
108    if (uploadInfo.helpMessage != null) {
109      ToolTip helpToolTip = new ToolTip("?", uploadInfo.helpMessage);
110      addWidget(helpToolTip, uploadInfo.helpToolTipId);
111    }
112  }
113
114  private static void rpcCall(
115      final String rpcName, final JSONObject params, final String successMessage) {
116    JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
117    rpcProxy.rpcCall(
118        rpcName,
119        params,
120        new JsonRpcCallback() {
121          @Override
122          public void onSuccess(JSONValue result) {
123            NotifyManager.getInstance().showMessage(successMessage);
124          }
125        });
126  }
127
128  /**
129   * Upload related information.
130   */
131  private static class UploadInfo {
132    final String keyName;
133    final String rpcName;
134    final String rpcArgName;
135    final String successMessage;
136    final String fileUploadName;
137    final String uploadViewId;
138    final String submitButtonId;
139    final String helpToolTipId;
140    final String helpMessage;
141
142    public UploadInfo(
143        String keyName,
144        String rpcName,
145        String rpcArgName,
146        String successMessage,
147        String helpMessage) {
148      this.keyName = keyName;
149      this.rpcName = rpcName;
150      this.rpcArgName = rpcArgName;
151      this.successMessage = successMessage;
152      this.fileUploadName = keyName;
153      this.uploadViewId = "view_" + this.keyName;
154      this.submitButtonId = "view_submit_" + this.keyName;
155      this.helpToolTipId = "view_help_" + this.keyName;
156      this.helpMessage = helpMessage;
157    }
158  }
159}
160