deploy_controller.js 3.4 KB
Newer Older
B
bryk 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * Controller for the deploy view.
 *
 * @final
 */
export default class DeployController {
  /**
23 24
   * @param {!angular.$resource} $resource
   * @param {!angular.$log} $log
25
   * @param {!ui.router.$state} $state
B
bryk 已提交
26 27
   * @ngInject
   */
28
  constructor($resource, $log, $state) {
29
    /** @export {string} */
30
    this.name = '';
B
bryk 已提交
31

32 33 34
    /** @export {string} */
    this.containerImage = '';

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    /** @export {number} */
    this.replicas = 1;

    /**
     * List of supported protocols.
     * TODO(bryk): Do not hardcode the here, move to backend.
     * @const @export {!Array<string>}
     */
    this.protocols = ['TCP', 'UDP'];

    /** @export {!Array<!backendApi.PortMapping>} */
    this.portMappings = [this.newEmptyPortMapping_(this.protocols[0])];

    /** @export {boolean} */
    this.isExternal = false;

    /** @private {!angular.Resource<!backendApi.AppDeployment>} */
52 53 54 55
    this.resource_ = $resource('/api/deploy');

    /** @private {!angular.$log} */
    this.log_ = $log;
56 57 58 59 60 61

    /** @private {!ui.router.$state} */
    this.state_ = $state;

    /** @private {boolean} */
    this.isDeployInProgress_ = false;
62 63 64 65 66 67 68 69
  }

  /**
   * Deploys the application based on the sate of the controller.
   *
   * @export
   */
  deploy() {
70 71 72
    // TODO(bryk): Validate input data before sending to the server.

    /** @type {!backendApi.AppDeployment} */
73 74
    let deployAppConfig = {
      containerImage: this.containerImage,
75 76 77 78
      isExternal: this.isExternal,
      name: this.name,
      portMappings: this.portMappings.filter(this.isPortMappingEmpty_),
      replicas: this.replicas,
79 80
    };

81
    this.isDeployInProgress_ = true;
82 83 84
    this.resource_.save(
        deployAppConfig,
        (savedConfig) => {
85 86
          this.isDeployInProgress_ = false;
          this.log_.info('Successfully deployed application: ', savedConfig);
87
          this.state_.go('replicasetlist');
88 89
        },
        (err) => {
90 91
          this.isDeployInProgress_ = false;
          this.log_.error('Error deploying application:', err);
92
        });
B
bryk 已提交
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

  /**
   * Returns true when the deploy action should be enabled.
   * @return {boolean}
   * @export
   */
  isDeployDisabled() {
    return this.isDeployInProgress_;
  }

  /**
   * Cancels the deployment form.
   * @export
   */
  cancel() {
    this.state_.go('zero');
  }

  /**
   * @param {string} defaultProtocol
   * @return {!backendApi.PortMapping}
   * @private
   */
  newEmptyPortMapping_(defaultProtocol) {
    return {
      port: null,
      targetPort: null,
      protocol: defaultProtocol,
    };
  }

  /**
   * Returns true when the given port mapping hasn't been filled by the user, i.e., is empty.
   * @param {!backendApi.PortMapping} portMapping
   * @return {boolean}
   * @private
   */
  isPortMappingEmpty_(portMapping) {
    return !!portMapping.port && !!portMapping.targetPort;
  }
B
bryk 已提交
134
}