• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package autotest.moblab.wizard;
2 
3 import com.google.gwt.event.logical.shared.ValueChangeEvent;
4 import com.google.gwt.event.logical.shared.ValueChangeHandler;
5 import com.google.gwt.json.client.JSONObject;
6 import com.google.gwt.user.client.ui.Anchor;
7 import com.google.gwt.user.client.ui.CheckBox;
8 import com.google.gwt.user.client.ui.Label;
9 import com.google.gwt.user.client.ui.TextBox;
10 
11 import autotest.common.Utils;
12 import autotest.moblab.rpc.CloudStorageInfo;
13 import autotest.moblab.rpc.MoblabRpcCallbacks;
14 import autotest.moblab.rpc.MoblabRpcHelper;
15 import autotest.moblab.rpc.OperationStatus;
16 import autotest.common.ui.ToolTip;
17 
18 import java.util.HashMap;
19 
20 /**
21  * Wizard card for cloud storage configuration.
22  */
23 public class CloudStorageCard extends FlexWizardCard {
24   /**
25    * The cached cloud storage information.
26    */
27   private CloudStorageInfo cloudStorageInfo;
28 
29   /**
30    * Checkbox for if reuse existing boto file.
31    */
32   private CheckBox chkUseExisting;
33 
CloudStorageCard()34   public CloudStorageCard() {
35     super();
36     setViewTitle("Google Cloud Storage Configuration");
37     setEditTitle("Configure Access to Google Cloud Storage");
38   }
39 
40   @Override
updateModeUI()41   protected void updateModeUI() {
42     if (cloudStorageInfo != null) {
43       resetUI();
44       int row = 0;
45       // In edit mode, display the check box for re-using existing boto file.
46       if (ConfigWizard.Mode.Edit == getMode()) {
47         chkUseExisting = new CheckBox("Use Existing Boto File on Moblab Device.");
48         layoutTable.setWidget(row, 1, chkUseExisting);
49         chkUseExisting.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
50           @Override
51           public void onValueChange(ValueChangeEvent<Boolean> event) {
52             if (cloudStorageInfo != null) {
53               cloudStorageInfo.setUseExistingBotoFile(event.getValue());
54             }
55             TextBox box = getValueFieldEditor(CloudStorageInfo.JSON_FIELD_BOTO_KEY_ID);
56             if (box != null) {
57               box.setEnabled(!event.getValue());
58             }
59             box = getValueFieldEditor(CloudStorageInfo.JSON_FIELD_BOTO_SECRET_KEY);
60             if (box != null) {
61               box.setEnabled(!event.getValue());
62             }
63           }
64         });
65 
66       }
67 
68       // Row for boto key id.
69       row++;
70       layoutTable.setWidget(row, 0, new Label("Boto Key ID"));
71       layoutTable.setWidget(row, 1, createValueFieldWidget(CloudStorageInfo.JSON_FIELD_BOTO_KEY_ID,
72           cloudStorageInfo.getBotoKey()));
73 
74       // Row for boto key secret.
75       row++;
76       layoutTable.setWidget(row, 0, new Label("Boto Key Secret"));
77       layoutTable.setWidget(row, 1, createStringValueFieldWidget(
78           CloudStorageInfo.JSON_FIELD_BOTO_SECRET_KEY, cloudStorageInfo.getBotoSecret(), true));
79 
80       // Row for image storage bucket url.
81       row++;
82       layoutTable.setWidget(row, 0, new Label("Image Storage Bucket URL"));
83       String url = cloudStorageInfo.getImageStorageServer();
84       layoutTable.setWidget(row, 1,
85           createValueFieldWidget(CloudStorageInfo.JSON_FIELD_IMAGE_STORAGE_URL, url));
86       if (url != null && ConfigWizard.Mode.View == getMode()) {
87         Anchor link = Utils.createGoogleStorageHttpUrlLink("link", url);
88         layoutTable.setWidget(row, 2, link);
89       }
90 
91       if (ConfigWizard.Mode.Edit == getMode()) {
92         chkUseExisting.setValue(cloudStorageInfo.isUseExistingBotoFile(), true);
93       }
94     } else {
95       MoblabRpcHelper.fetchCloudStorageInfo(new MoblabRpcCallbacks.FetchCloudStorageInfoCallback() {
96         @Override
97         public void onCloudStorageInfoFetched(CloudStorageInfo info) {
98           cloudStorageInfo = info;
99           updateModeUI();
100         }
101       });
102     }
103   }
104 
105   @Override
validate(final CardValidationCallback callback)106   public void validate(final CardValidationCallback callback) {
107     // If not use existing boto, than boto id and boto secret fields can not be empty.
108     if (!chkUseExisting.getValue()) {
109       cloudStorageInfo
110           .setBotoKey(getStringValueFieldValue(CloudStorageInfo.JSON_FIELD_BOTO_KEY_ID));
111       cloudStorageInfo
112           .setBotoSecret(getStringValueFieldValue(CloudStorageInfo.JSON_FIELD_BOTO_SECRET_KEY));
113     }
114     cloudStorageInfo.setImageStorageServer(
115           getStringValueFieldValue(CloudStorageInfo.JSON_FIELD_IMAGE_STORAGE_URL));
116     if (!chkUseExisting.getValue()) {
117       // Boto key and secret are required.
118       if (cloudStorageInfo.getBotoKey() == null || cloudStorageInfo.getBotoSecret() == null) {
119         callback.onValidationStatus(
120             new OperationStatus(false, "The boto key fields could not be empty"));
121         return;
122       }
123       // Image bucket and result bucket can not be empty.
124       if (cloudStorageInfo.getImageStorageServer() == null) {
125         callback.onValidationStatus(
126             new OperationStatus(false, "The image bucket URL fields could not be empty"));
127         return;
128       }
129     }
130     // Image bucket and result bucket must end in /.
131     if (cloudStorageInfo.getImageStorageServer().substring(
132       cloudStorageInfo.getImageStorageServer().length() - 1) != "/") {
133       callback.onValidationStatus(
134         new OperationStatus(false, "The image bucket URL must end in /"));
135       return;
136     }
137 
138     // Sends validation request to server to validate the boto key and bucket urls.
139     MoblabRpcHelper.validateCloudStorageInfo(cloudStorageInfo,
140         new MoblabRpcCallbacks.ValidateCloudStorageInfoCallback() {
141           @Override
142           public void onCloudStorageInfoValidated(OperationStatus status) {
143             if (!status.isOk()) {
144               callback.onValidationStatus(status);
145               return;
146             }
147             CloudStorageCard.super.validate(callback);
148           }
149         });
150     return;
151   }
152 
153   /**
154    * Gets the string input field value.
155    */
getStringValueFieldValue(String fieldId)156   protected String getStringValueFieldValue(String fieldId) {
157     TextBox textBox = getValueFieldEditor(fieldId);
158     String value = textBox.getValue();
159     if (value != null) {
160       value = value.trim();
161     }
162 
163     if (value == null || value.length() == 0) {
164       return null;
165     }
166     return value;
167   }
168 
169   @Override
resetData()170   public void resetData() {
171     cloudStorageInfo = null;
172     super.resetData();
173   }
174 
175   @Override
collectConfigData(@uppressWarnings"unused") HashMap<String, JSONObject> map)176   public void collectConfigData(@SuppressWarnings("unused") HashMap<String, JSONObject> map) {
177     if (map != null && cloudStorageInfo != null) {
178       map.put(MoblabRpcHelper.RPC_PARAM_CLOUD_STORAGE_INFO, cloudStorageInfo.toJson());
179     }
180   }
181 }
182