<commit_before>import Ember from 'ember';
import { regions, sizes, storageTypes } from 'ui/utils/azure-choices';
import Driver from 'ui/mixins/driver';

export default Ember.Component.extend(Driver, {
  azureConfig      : Ember.computed.alias('model.azureConfig'),
  regionChoices    : regions.sortBy('name'),
  sizeChoices      : sizes.sort(),
  driverName       : 'azure',
  model            : null,
  openPorts        : null,
  publicIpChoice   : null,
  publicIpChoices  : [
    {
      'name': 'Static',
      'value': 'staticPublicIp=true,noPublicIp=false'
    },
    {
      'name': 'Dynamic',
      'value': 'staticPublicIp=false,noPublicIp=false'
    },
    {
      'name': 'None',
      'value': 'staticPublicIp=true,noPublicIp=true'
    },
  ],
  storageTypeChoices: storageTypes.sortBy('name'),

  bootstrap: function() {
    let config = this.get('store').createRecord({
      type             : 'azureConfig',
      subscriptionId   : '',
      clientId         : '',
      clientSecret     : '',
    });

    this.set('model', this.get('store').createRecord({
      type: 'host',
      azureConfig: config,
    }));

    this.set('editing', false);
  },

  init() {
    this._super(...arguments);

    this.set('publicIpChoice', this.initPublicIpChoices(this.get('azureConfig.staticPublicIp'), this.get('azureConfig.noPublicIp')));
    this.set('openPorts', this.initOpenPorts(this.get('azureConfig.openPort')));
  },

  initOpenPorts: function(ports) {
    return ports ? ports.join(',') : '';
  },

  initPublicIpChoices: function(staticPublicIp, noPublicIp) {
    if (staticPublicIp && noPublicIp) {
      return this.get('publicIpChoices').findBy('name', 'None').value;
    } else if (staticPublicIp && !noPublicIp) {
      return this.get('publicIpChoices').findBy('name', 'Static').value;
    } else {
      return this.get('publicIpChoices').findBy('name', 'Dynamic').value;
    }
  },

  setUsePrivateIp: Ember.computed('publicIpChoice', function() {
      let publicIpChoice = this.get('publicIpChoice');
      if (this.get('publicIpChoices').findBy('value', publicIpChoice).name === 'None') {
        return this.set('azureConfig.usePrivateIp', true);
      }
      return this.set('azureConfig.usePrivateIp', false);
  }),

  publicIpObserver: Ember.observer('publicIpChoice', function() {
    let elChoice = this.get('publicIpChoice');
    let choice = this.get('publicIpChoices').findBy('value', elChoice);

    choice = choice.value.split(',');

    choice.forEach((val) => {
      let tmp = val.split('=');
      this.set(`azureConfig.${tmp[0]}`, tmp[1] === 'true' ? true : false);
    });

  }),

  openPort: Ember.observer('openPorts', function() {
    this.set('azureConfig.openPort', this.get('openPorts').split(','));
  }),

  validate: function() {
    let errors = [];

    if (!this.get('azureConfig.subscriptionId') ) {
      errors.push('Subscription ID is required');
    }
<commit_msg>Azure has naming rules and fails with unhelpful messages<commit_after>import Ember from 'ember';
import { regions, sizes, storageTypes } from 'ui/utils/azure-choices';
import Driver from 'ui/mixins/driver';

export default Ember.Component.extend(Driver, {
  azureConfig      : Ember.computed.alias('model.azureConfig'),
  regionChoices    : regions.sortBy('name'),
  sizeChoices      : sizes.sort(),
  driverName       : 'azure',
  model            : null,
  openPorts        : null,
  publicIpChoice   : null,
  publicIpChoices  : [
    {
      'name': 'Static',
      'value': 'staticPublicIp=true,noPublicIp=false'
    },
    {
      'name': 'Dynamic',
      'value': 'staticPublicIp=false,noPublicIp=false'
    },
    {
      'name': 'None',
      'value': 'staticPublicIp=true,noPublicIp=true'
    },
  ],
  storageTypeChoices: storageTypes.sortBy('name'),

  bootstrap: function() {
    let config = this.get('store').createRecord({
      type             : 'azureConfig',
      subscriptionId   : '',
      clientId         : '',
      clientSecret     : '',
    });

    this.set('model', this.get('store').createRecord({
      type: 'host',
      azureConfig: config,
    }));

    this.set('editing', false);
  },

  init() {
    this._super(...arguments);

    this.set('publicIpChoice', this.initPublicIpChoices(this.get('azureConfig.staticPublicIp'), this.get('azureConfig.noPublicIp')));
    this.set('openPorts', this.initOpenPorts(this.get('azureConfig.openPort')));
  },

  initOpenPorts: function(ports) {
    return ports ? ports.join(',') : '';
  },

  initPublicIpChoices: function(staticPublicIp, noPublicIp) {
    if (staticPublicIp && noPublicIp) {
      return this.get('publicIpChoices').findBy('name', 'None').value;
    } else if (staticPublicIp && !noPublicIp) {
      return this.get('publicIpChoices').findBy('name', 'Static').value;
    } else {
      return this.get('publicIpChoices').findBy('name', 'Dynamic').value;
    }
  },

  setUsePrivateIp: Ember.computed('publicIpChoice', function() {
      let publicIpChoice = this.get('publicIpChoice');
      if (this.get('publicIpChoices').findBy('value', publicIpChoice).name === 'None') {
        return this.set('azureConfig.usePrivateIp', true);
      }
      return this.set('azureConfig.usePrivateIp', false);
  }),

  publicIpObserver: Ember.observer('publicIpChoice', function() {
    let elChoice = this.get('publicIpChoice');
    let choice = this.get('publicIpChoices').findBy('value', elChoice);

    choice = choice.value.split(',');

    choice.forEach((val) => {
      let tmp = val.split('=');
      this.set(`azureConfig.${tmp[0]}`, tmp[1] === 'true' ? true : false);
    });

  }),

  openPort: Ember.observer('openPorts', function() {
    this.set('azureConfig.openPort', this.get('openPorts').split(','));
  }),

  validate: function() {
    let errors = [];

    this.set('prefix',(this.get('prefix')||'').toLowerCase());
    let name = this.get('model.name');
    if ( name.length < 4 || name > 62 ) {
      errors.push('Name must be between 4 and 62 characters long');
    }

    if ( name.match(/[^a-z0-9-]/) ) {
      errors.push('Name can only contain a-z, 0-9, and hyphen');
    }

    if (!this.get('azureConfig.subscriptionId') ) {
      errors.push('Subscription ID is required');
    }

{'fraction_non_alphanumeric': 0.1752328943141664, 'fraction_numerical': 0.0022486347574686796, 'mean_word_length': 3.279725085910653, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6606440', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1960, 'n_words': 332}