summaryrefslogtreecommitdiffstats
path: root/acquiasdk.module
blob: 3c2edb4e2508f353284c2dd9705d5daa2e8f8139 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
 * @file
 * Acquia SDK module functions.
 */

use Acquia\Cloud\Api\CloudApiClient;
use Acquia\Network\AcquiaNetworkClient;
use Acquia\Common\Services;

/**
 * Implements hook_menu().
 */
function acquiasdk_menu() {
  $items = array();

  $items['admin/config/development/acquiasdk'] = array(
    'title' => 'Acquia Cloud API Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('acquiasdk_settings'),
    'access arguments' => array('administer acquiasdk'),
  );

  return $items;
}

/**
 * Implements hook_permission().
 */
function acquiasdk_permission() {
  $perms = array();

  $parms['administer acquiasdk'] = array(
    'title' => t('Administer Acquia SDK'),
    'description' => t('Perform administration functions for Acquia SDK module.'),
  );

  return $perms;
}

/**
 * Acquia SDK module settings form.
 */
function acquiasdk_settings($form, &$form_state) {
  $form = array();

  $account_link = l(t('Acquia account page'), 'https://accounts.acquia.com/account', array('attributes' => array('target' => '_blank')));
  $form['cloudapi'] = array(
    '#type' => 'fieldset',
    '#title' => t('Cloud API'),
    '#description' => t('Obtain your Acquia Cloud API credentials from your !link.', array('!link' => $account_link)),
  );

  $form['cloudapi']['acquiasdk_cloudapi_username'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#default_value' => variable_get('acquiasdk_cloudapi_username'),
  );

  $form['cloudapi']['acquiasdk_cloudapi_password'] = array(
    '#type' => 'password',
    '#title' => t('Password'),
    '#description' => t('Leave this blank to keep the existing password.'),
  );

  $network_link = l(t('Acquia Insight'), 'https://insight.acquia.com/', array('attributes' => array('target' => '_blank')));
  $form['network'] = array(
    '#type' => 'fieldset',
    '#title' => t('Acquia Network'),
    '#description' => t('Obtain your Acquia Network credentials from !link.', array('!link' => $network_link)),
  );

  $form['network']['acquiasdk_network_id'] = array(
    '#type' => 'textfield',
    '#title' => t('ID'),
    '#default_value' => variable_get('acquiasdk_network_id'),
  );

  $form['network']['acquiasdk_network_key'] = array(
    '#type' => 'password',
    '#title' => t('Key'),
    '#description' => t('Leave this blank to keep the existing key.'),
  );

  $form = system_settings_form($form);
  return $form;
}

/**
 * Validates Acquia SDK module settings form.
 */
function acquiasdk_settings_validate($form, &$form_state) {
  $password_fields = array(
    'acquiasdk_cloudapi_password' => 'acquiasdk_cloudapi_username',
    'acquiasdk_network_key' => 'acquiasdk_network_id',
  );

  foreach ($password_fields as $password_field => $user_field) {
    $field =& $form_state['values'][$password_field];
    if (!$field) {
      // Hack to preserve the existing value if no new value is set.
      unset($form_state['values'][$password_field]);
      continue;
    }

    if (!$form_state['values'][$user_field]) {
      form_set_error($user_field, t('Username is required when providing a password'));
    }

    if (module_exists('encrypt')) {
      $field = encrypt($field);
    }
  }
}

/**
 * Instantiates a Acquia Cloud API client object.
 *
 * @param string $username
 *   (optional) Acquia Cloud API username. If NULL then global config is used.
 * @param string $password
 *   (optional) Acquia Cloud API password. If NULL then global config is used.
 *
 * @return CloudApiClient
 *   Acquia Cloud API client object.
 */
function acquiasdk_get_cloudapi_client($username = NULL, $password = NULL) {
  if (!$username) {
    $username = variable_get('acquiasdk_cloudapi_username');
  }

  if (!$password) {
    $password = acquiasdk_variable_get('acquiasdk_cloudapi_password');
  }

  if (!$username || !$password) {
    throw new Exception('Invalid Acquia Cloud API credentials.');
  }

  $cloudapi = CloudApiClient::factory(array(
    'username' => $username,
    'password' => $password,
  ));

  return $cloudapi;
}

/**
 * Instantiates Acquia Network client object.
 *
 * @param string $network_id
 *   Acquia Network id. If NULL then global config is used. (optional)
 * @param string $network_key
 *   Acquia Network key. If NULL then global config is used. (optional)
 *
 * @return AcquiaNetworkClient
 *   Acquia Network client object.
 */
function acquiasdk_get_network_client($network_id = NULL, $network_key = NULL) {
  if (!$network_id) {
    $network_id = variable_get('acquiasdk_network_id');
  }

  if (!$password) {
    $network_key = acquiasdk_variable_get('acquiasdk_network_key');
  }

  if (!$network_id || !$network_key) {
    throw new Exception('Invalid Acquia Network credentials.');
  }

  $network = AcquiaNetworkClient::factory(array(
    'network_id' => $network_id,
    'network_key' => $network_key,
  ));

  return $network;
}

/**
 * Retrieves variable with transparent unencryption if supported.
 *
 * @param string $name
 *   The name of the variable to return.
 *
 * @return mixed
 *   The value of the variable or NULL if not found.
 */
function acquiasdk_variable_get($name) {
  $encrypted = &drupal_static(__FUNCTION__);
  if (NULL == $encrypted) {
    $encrypted = module_exists('encrypt');
  }

  $var = variable_get($name);
  if (!$encrypted || 'a:3' != substr($var, 0, 3)) {
    return $var;
  }

  return decrypt($var);
}