Module: Gzr::Attribute

Instance Method Summary collapse

Instance Method Details

#create_attribute(attr) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/gzr/modules/attribute.rb', line 69

def create_attribute(attr)
  begin
    @sdk.create_user_attribute(attr).to_attrs
  rescue LookerSDK::Error => e
    say_error "Error creating user_attribute(#{JSON.pretty_generate(attr)})"
    say_error e
    raise
  end
end

#delete_user_attribute(id) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gzr/modules/attribute.rb', line 93

def delete_user_attribute(id)
  begin
    @sdk.delete_user_attribute(id)
  rescue LookerSDK::NotFound => e
    say_error "user_attribute #{id} not found"
    say_error e
    raise
  rescue LookerSDK::Error => e
    say_error "Error deleting user_attribute(#{id})"
    say_error e
    raise
  end
end

#get_attribute_by_label(label, fields = nil) ⇒ Object



63
64
65
66
67
# File 'lib/gzr/modules/attribute.rb', line 63

def get_attribute_by_label(label, fields = nil)
  data = query_all_user_attributes(fields).select {|a| a[:label] == label}
  return nil if data.empty?
  data.first
end

#get_attribute_by_name(name, fields = nil) ⇒ Object



57
58
59
60
61
# File 'lib/gzr/modules/attribute.rb', line 57

def get_attribute_by_name(name, fields = nil)
  data = query_all_user_attributes(fields).select {|a| a[:name] == name}
  return nil if data.empty?
  data.first
end

#query_all_user_attribute_group_values(attr_id, fields = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/gzr/modules/attribute.rb', line 107

def query_all_user_attribute_group_values(attr_id, fields=nil)
  begin
    req = {}
    req[:fields] = fields if fields
    return @sdk.all_user_attribute_group_values(attr_id,req).collect { |v| v.to_attrs }
  rescue LookerSDK::NotFound => e
    return nil
  rescue LookerSDK::Error => e
    say_error "Error querying all_user_attribute_group_values(#{attr_id},#{JSON.pretty_generate(req)})"
    say_error e
    raise
  end
end

#query_all_user_attributes(fields = nil, sorts = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gzr/modules/attribute.rb', line 42

def query_all_user_attributes(fields=nil, sorts=nil)
  begin
    req = {}
    req[:fields] = fields if fields
    req[:sorts] = sorts if sorts
    @sdk.all_user_attributes(req).collect { |a| a.to_attrs }
  rescue LookerSDK::NotFound => e
    []
  rescue LookerSDK::Error => e
    say_error "Error querying all_user_attributes(#{JSON.pretty_generate(req)})"
    say_error e
    raise
  end
end

#query_user_attribute(attr_id, fields = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gzr/modules/attribute.rb', line 26

def query_user_attribute(attr_id,fields=nil)
  begin
    req = {}
    req[:fields] = fields if fields
    @sdk.user_attribute(attr_id,req).to_attrs
  rescue LookerSDK::NotFound => e
    say_error "User_attribute #{attr_id} not found"
    say_error e
    raise
  rescue LookerSDK::Error => e
    say_error "Error querying user_attribute(#{attr_id},#{JSON.pretty_generate(req)})"
    say_error e
    raise
  end
end

#query_user_attribute_group_value(group_id, attr_id) ⇒ Object



121
122
123
124
125
# File 'lib/gzr/modules/attribute.rb', line 121

def query_user_attribute_group_value(group_id, attr_id)
  data = query_all_user_attribute_group_values(attr_id)&.select {|a| a[:group_id] == group_id}
  return nil if data.nil? || data.empty?
  data.first
end

#update_attribute(id, attr) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gzr/modules/attribute.rb', line 79

def update_attribute(id,attr)
  begin
    @sdk.update_user_attribute(id,attr).to_attrs
  rescue LookerSDK::NotFound => e
    say_error "user_attribute #{id} not found"
    say_error e
    raise
  rescue LookerSDK::Error => e
    say_error "Error updating user_attribute(#{id},#{JSON.pretty_generate(attr)})"
    say_error e
    raise
  end
end

#upsert_user_attribute(source, force = false, output: $stdout) ⇒ Object



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
153
154
155
# File 'lib/gzr/modules/attribute.rb', line 127

def upsert_user_attribute(source, force=false, output: $stdout)
  name_used = get_attribute_by_name(source[:name])
  if name_used
    raise(Gzr::CLI::Error, "Attribute #{source[:name]} already exists and can't be modified") if name_used[:is_system]
    raise(Gzr::CLI::Error, "Attribute #{source[:name]} already exists\nUse --force if you want to overwrite it") unless @options[:force]
  end

  label_used = get_attribute_by_label(source[:label])
  if label_used
    raise(Gzr::CLI::Error, "Attribute with label #{source[:label]} already exists and can't be modified") if label_used[:is_system]
    raise(Gzr::CLI::Error, "Attribute with label #{source[:label]} already exists\nUse --force if you want to overwrite it") unless force
  end

  existing = name_used || label_used
  if existing
    upd_attr = source.select do |k,v|
      keys_to_keep('update_user_attribute').include?(k) && !(name_used[k] == v)
    end

    return update_attribute(existing[:id],upd_attr)
  else
    new_attr = source.select do |k,v|
      (keys_to_keep('create_user_attribute') - [:hidden_value_domain_whitelist]).include? k
    end
    new_attr[:hidden_value_domain_whitelist] = source[:hidden_value_domain_allowlist] if source[:value_is_hidden] && source[:hidden_value_domain_allowlist]

    return create_attribute(new_attr)
  end
end