<commit_before>/*
This file is part of the Juju GUI, which lets users view and manage Juju
environments within a graphical interface (https://launchpad.net/juju-gui).
Copyright (C) 2016 Canonical Ltd.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License version 3, as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
General Public License for more details.

You should have received a copy of the GNU Affero General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

'use strict';

YUI.add('deployment-credential', function() {

  juju.components.DeploymentCredential = React.createClass({
    propTypes: {
      acl: React.PropTypes.object.isRequired,
      addTemplate: React.PropTypes.func.isRequired,
      cloud: React.PropTypes.object,
      clouds: React.PropTypes.object.isRequired,
      credential: React.PropTypes.string,
      getCloudCredentials: React.PropTypes.func.isRequired,
      getTagsForCloudCredentials: React.PropTypes.func.isRequired,
      listRegions: React.PropTypes.func.isRequired,
      region: React.PropTypes.string,
      setCredential: React.PropTypes.func.isRequired,
      setRegion: React.PropTypes.func.isRequired,
      setTemplate: React.PropTypes.func.isRequired,
      template: React.PropTypes.string,
      users: React.PropTypes.object.isRequired,
      validateForm: React.PropTypes.func.isRequired
    },

    regionsXHR: null,

    getInitialState: function() {
      return {
        credentials: [],
        credentialsLoading: true,
        regions: [],
        regionsLoading: true,
        showAdd: true
      };
    },

    componentWillMount: function() {
      this._getCredentials();
      this._getRegions();
    },

    componentDidUpdate: function(prevProps) {
      const prevId = prevProps.cloud && prevProps.cloud.id;
      const newId = this.props.cloud && this.props.cloud.id;
      if (newId !== prevId) {
        this._getCredentials();
        this._getRegions();
      }
    },

    componentWillUnmount: function() {
      if (this.regionsXHR) {
        this.regionsXHR.abort();
      }
    },

    /**
      Request credentials from JEM.

      @method _getCredentials
    */
    _getCredentials: function() {
      const cloud = this.props.cloud && this.props.cloud.id;
      const user = this.props.users.jem.user;
      if (user) {
        this.props.getTagsForCloudCredentials(
          [[user, cloud]], this._getTagsCallback);
      }
    },

    /**
      The method to be called when the credentials tags reponse has been
      received.

      @method _getTagsCallback
      @param {String} error An error message, or null if there's no error.
      @param {Array} tags A list of the tags found.
    */
    _getTagsCallback: function(error, tags) {
      if (error) {
        console.error('Unable to get tags for credentials', error);
        return;
      }
      const tagList = [];
      tags.forEach(tag => {
        tagList = tagList.concat(tag.tags);
      });
      this.props.getCloudCredentials(tagList, this._getCredentialsCallback);
    },

    /**
      The method to be called when the credentials response has been received.

      @method _getCredentialsCallback<commit_msg>Correctly get the tag results.<commit_after>/*
This file is part of the Juju GUI, which lets users view and manage Juju
environments within a graphical interface (https://launchpad.net/juju-gui).
Copyright (C) 2016 Canonical Ltd.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License version 3, as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
General Public License for more details.

You should have received a copy of the GNU Affero General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

'use strict';

YUI.add('deployment-credential', function() {

  juju.components.DeploymentCredential = React.createClass({
    propTypes: {
      acl: React.PropTypes.object.isRequired,
      addTemplate: React.PropTypes.func.isRequired,
      cloud: React.PropTypes.object,
      clouds: React.PropTypes.object.isRequired,
      credential: React.PropTypes.string,
      getCloudCredentials: React.PropTypes.func.isRequired,
      getTagsForCloudCredentials: React.PropTypes.func.isRequired,
      listRegions: React.PropTypes.func.isRequired,
      region: React.PropTypes.string,
      setCredential: React.PropTypes.func.isRequired,
      setRegion: React.PropTypes.func.isRequired,
      setTemplate: React.PropTypes.func.isRequired,
      template: React.PropTypes.string,
      users: React.PropTypes.object.isRequired,
      validateForm: React.PropTypes.func.isRequired
    },

    regionsXHR: null,

    getInitialState: function() {
      return {
        credentials: [],
        credentialsLoading: true,
        regions: [],
        regionsLoading: true,
        showAdd: true
      };
    },

    componentWillMount: function() {
      this._getCredentials();
      this._getRegions();
    },

    componentDidUpdate: function(prevProps) {
      const prevId = prevProps.cloud && prevProps.cloud.id;
      const newId = this.props.cloud && this.props.cloud.id;
      if (newId !== prevId) {
        this._getCredentials();
        this._getRegions();
      }
    },

    componentWillUnmount: function() {
      if (this.regionsXHR) {
        this.regionsXHR.abort();
      }
    },

    /**
      Request credentials from JEM.

      @method _getCredentials
    */
    _getCredentials: function() {
      const cloud = this.props.cloud && this.props.cloud.id;
      const user = this.props.users.jem.user;
      if (user) {
        this.props.getTagsForCloudCredentials(
          [[user, cloud]], this._getTagsCallback);
      }
    },

    /**
      The method to be called when the credentials tags reponse has been
      received.

      @method _getTagsCallback
      @param {String} error An error message, or null if there's no error.
      @param {Array} tags A list of the tags found.
    */
    _getTagsCallback: function(error, tags) {
      if (error) {
        console.error('Unable to get tags for credentials', error);
        return;
      }
      // The resulting array of tags will be in the order that the cloud/user
      // pairs were passed to getTagsForCloudCredentials. As we're only passing
      // one pair we can safely assume that we only need the first item in the
      // array.
      const tagList = tags && tags[0] && tags[0].tags || [];
      this.props.getCloudCredentials(tagList, this._getCredentialsCallback);
    },

    /**
      The method to be called when the credentials response has been received.

      @method _getCredentialsCallback
{'fraction_non_alphanumeric': 0.0939615862926627, 'fraction_numerical': 0.0016581456404587537, 'mean_word_length': 2.8336864406779663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7062384', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1994, 'n_words': 611}