SetSetting set the setting by name.
(ctx context.Context, request *connect.Request[v1pb.UpdateSettingRequest])
| 126 | |
| 127 | // SetSetting set the setting by name. |
| 128 | func (s *SettingService) UpdateSetting(ctx context.Context, request *connect.Request[v1pb.UpdateSettingRequest]) (*connect.Response[v1pb.Setting], error) { |
| 129 | user, ok := GetUserFromContext(ctx) |
| 130 | if !ok { |
| 131 | return nil, connect.NewError(connect.CodeInternal, errors.Errorf("user not found")) |
| 132 | } |
| 133 | |
| 134 | settingName, err := common.GetSettingName(request.Msg.Setting.Name) |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | if settingName == "" { |
| 139 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("setting name is empty")) |
| 140 | } |
| 141 | storeSettingName, err := convertStringToSettingName(settingName) |
| 142 | if err != nil { |
| 143 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid setting name: %v", err)) |
| 144 | } |
| 145 | if s.isSettingDisallowed(storeSettingName) { |
| 146 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("setting is not available")) |
| 147 | } |
| 148 | if s.profile.IsFeatureUnavailable(settingName) { |
| 149 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("feature %s is unavailable in current mode", settingName)) |
| 150 | } |
| 151 | |
| 152 | var perm permission.Permission |
| 153 | switch storeSettingName { |
| 154 | case storepb.SettingName_ENVIRONMENT: |
| 155 | perm = permission.EnvironmentSettingsSet |
| 156 | case storepb.SettingName_WORKSPACE_PROFILE: |
| 157 | perm = permission.WorkspaceProfileSettingsSet |
| 158 | default: |
| 159 | perm = permission.SettingsSet |
| 160 | } |
| 161 | if err := s.checkSettingPermission(ctx, request, perm); err != nil { |
| 162 | return nil, err |
| 163 | } |
| 164 | |
| 165 | workspaceID := common.GetWorkspaceIDFromContext(ctx) |
| 166 | existedSetting, err := s.store.GetSetting(ctx, workspaceID, storeSettingName) |
| 167 | if err != nil { |
| 168 | return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to find setting %s with error: %v", settingName, err)) |
| 169 | } |
| 170 | if existedSetting == nil && !request.Msg.AllowMissing { |
| 171 | return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("setting %s not found", settingName)) |
| 172 | } |
| 173 | // audit log. |
| 174 | if setServiceData, ok := common.GetSetServiceDataFromContext(ctx); ok && existedSetting != nil { |
| 175 | v1pbSetting, err := convertToSettingMessage(existedSetting) |
| 176 | if err != nil { |
| 177 | slog.Warn("audit: failed to convert to v1.Setting", log.BBError(err)) |
| 178 | } |
| 179 | p, err := anypb.New(v1pbSetting) |
| 180 | if err != nil { |
| 181 | slog.Warn("audit: failed to convert to anypb.Any", log.BBError(err)) |
| 182 | } |
| 183 | setServiceData(p) |
| 184 | } |
| 185 |
nothing calls this directly
no test coverage detected