summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_merge_request_widget/components/mr_widget_terraform_plan.vue
blob: edf90085a5b85d6db028487c0f411108b875a4c2 (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
<script>
import { __ } from '~/locale';
import { GlIcon, GlLoadingIcon, GlSprintf } from '@gitlab/ui';
import axios from '~/lib/utils/axios_utils';
import CiIcon from '../../vue_shared/components/ci_icon.vue';
import flash from '~/flash';
import Poll from '~/lib/utils/poll';
import Visibility from 'visibilityjs';

export default {
  name: 'MRWidgetTerraformPlan',
  components: {
    CiIcon,
    GlIcon,
    GlLoadingIcon,
    GlSprintf,
  },
  props: {
    endpoint: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      loading: true,
      plans: {},
    };
  },
  computed: {
    addNum() {
      return Number(this.plan.create);
    },
    changeNum() {
      return Number(this.plan.update);
    },
    deleteNum() {
      return Number(this.plan.delete);
    },
    iconStatusObj() {
      return {
        group: 'warning',
        icon: 'status_warning',
      };
    },
    logUrl() {
      return this.plan.job_path;
    },
    plan() {
      return this.plans['tfplan.json'] || {};
    },
    validPlanValues() {
      return this.addNum + this.changeNum + this.deleteNum >= 0;
    },
  },
  created() {
    this.fetchPlans();
  },
  methods: {
    fetchPlans() {
      this.loading = true;

      const poll = new Poll({
        resource: {
          fetchPlans: () => axios.get(this.endpoint),
        },
        data: this.endpoint,
        method: 'fetchPlans',
        successCallback: ({ data }) => {
          this.plans = data;
          this.loading = false;
        },
        errorCallback: () => {
          this.plans = {};
          this.loading = false;
          flash(__('An error occurred while loading terraform report'));
        },
      });

      if (!Visibility.hidden()) {
        poll.makeRequest();
      }

      Visibility.change(() => {
        if (!Visibility.hidden()) {
          poll.restart();
        } else {
          poll.stop();
        }
      });
    },
  },
};
</script>

<template>
  <section class="mr-widget-section">
    <div class="mr-widget-body media d-flex flex-row">
      <span class="append-right-default align-self-start align-self-lg-center">
        <ci-icon :status="iconStatusObj" :size="24" />
      </span>

      <div class="d-flex flex-fill flex-column flex-md-row">
        <div class="terraform-mr-plan-text normal d-flex flex-column flex-lg-row">
          <p class="m-0 pr-1">{{ __('A terraform report was generated in your pipelines.') }}</p>

          <gl-loading-icon v-if="loading" size="md" />

          <p v-else-if="validPlanValues" class="m-0">
            <gl-sprintf
              :message="
                __(
                  'Reported Resource Changes: %{addNum} to add, %{changeNum} to change, %{deleteNum} to delete',
                )
              "
            >
              <template #addNum>
                <strong>{{ addNum }}</strong>
              </template>

              <template #changeNum>
                <strong>{{ changeNum }}</strong>
              </template>

              <template #deleteNum>
                <strong>{{ deleteNum }}</strong>
              </template>
            </gl-sprintf>
          </p>

          <p v-else class="m-0">{{ __('Changes are unknown') }}</p>
        </div>

        <div class="terraform-mr-plan-actions">
          <a
            v-if="logUrl"
            :href="logUrl"
            target="_blank"
            data-track-event="click_terraform_mr_plan_button"
            data-track-label="mr_widget_terraform_mr_plan_button"
            data-track-property="terraform_mr_plan_button"
            class="btn btn-sm js-terraform-report-link"
            rel="noopener"
          >
            {{ __('View full log') }}
            <gl-icon name="external-link" />
          </a>
        </div>
      </div>
    </div>
  </section>
</template>