summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/environments/components/kubernetes_agent_info.vue
blob: c4f6d2254447ab39970c676e017386564c85bddc (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
<script>
import { GlIcon, GlLink, GlSprintf, GlLoadingIcon, GlAlert } from '@gitlab/ui';
import TimeAgoTooltip from '~/vue_shared/components/time_ago_tooltip.vue';
import { getAgentLastContact, getAgentStatus } from '~/clusters_list/clusters_util';
import { TOKEN_STATUS_ACTIVE } from '~/clusters/agents/constants';
import { AGENT_STATUSES } from '~/clusters_list/constants';
import { s__ } from '~/locale';
import getK8sClusterAgentQuery from '../graphql/queries/k8s_cluster_agent.query.graphql';

export default {
  components: {
    GlIcon,
    GlLink,
    GlSprintf,
    GlLoadingIcon,
    TimeAgoTooltip,
    GlAlert,
  },
  props: {
    agentName: {
      required: true,
      type: String,
    },
    agentId: {
      required: true,
      type: String,
    },
    agentProjectPath: {
      required: true,
      type: String,
    },
  },
  apollo: {
    clusterAgent: {
      query: getK8sClusterAgentQuery,
      variables() {
        return {
          agentName: this.agentName,
          projectPath: this.agentProjectPath,
          tokenStatus: TOKEN_STATUS_ACTIVE,
        };
      },
      update: (data) => data?.project?.clusterAgent,
      error() {
        this.clusterAgent = null;
      },
    },
  },
  data() {
    return {
      clusterAgent: null,
    };
  },
  computed: {
    isLoading() {
      return this.$apollo.queries.clusterAgent.loading;
    },
    agentLastContact() {
      return getAgentLastContact(this.clusterAgent.tokens.nodes);
    },
    agentStatus() {
      return getAgentStatus(this.agentLastContact);
    },
  },
  methods: {},
  i18n: {
    loadingError: s__('ClusterAgents|An error occurred while loading your agent'),
    agentId: s__('ClusterAgents|Agent ID #%{agentId}'),
    neverConnectedText: s__('ClusterAgents|Never'),
  },
  AGENT_STATUSES,
};
</script>
<template>
  <gl-loading-icon v-if="isLoading" inline />
  <div v-else-if="clusterAgent" class="gl-text-gray-900">
    <gl-icon name="kubernetes-agent" class="gl-text-gray-500" />
    <gl-link :href="clusterAgent.webPath" class="gl-mr-3">
      <gl-sprintf :message="$options.i18n.agentId"
        ><template #agentId>{{ agentId }}</template></gl-sprintf
      >
    </gl-link>
    <span class="gl-mr-3" data-testid="agent-status">
      <gl-icon
        :name="$options.AGENT_STATUSES[agentStatus].icon"
        :class="$options.AGENT_STATUSES[agentStatus].class"
      />
      {{ $options.AGENT_STATUSES[agentStatus].name }}
    </span>

    <span data-testid="agent-last-used-date">
      <gl-icon name="calendar" />
      <time-ago-tooltip v-if="agentLastContact" :time="agentLastContact" />
      <span v-else>{{ $options.i18n.neverConnectedText }}</span>
    </span>
  </div>

  <gl-alert v-else variant="danger" :dismissible="false">
    {{ $options.i18n.loadingError }}
  </gl-alert>
</template>