| // Copyright 2018 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package tilo |
| |
| import ( |
| "fmt" |
| |
| "fuchsia.googlesource.com/infra/infra/tilo/resultstore" |
| ) |
| |
| // LoggerState stores information necessary to create an Invocation. It is used to support |
| // tilo clients that use a distributed execution model, where multiple processes |
| // participate in creating, modifying, and finishing an Invocation. This can be |
| // accomplished by serializing the LoggerState as a JSON object and passing it to another |
| // process. |
| // |
| // When creating ResultStore entities, the name of the entity's parent must be provided |
| // for ResultStore to make the parent-child association. The entity name is created by |
| // the ResultStore backend and returned from the UploadApi methods. This cache is used to |
| // store those names as they are returned. |
| // |
| // All requests to modify an Invocation require an authorization token, which is specified |
| // when the Invocation is created. This cache also stores that token. |
| // |
| // LoggerState getters return false if values are not present, or empty. Setters return false |
| // when values are already set. Attempting to ovewrite a value should always be |
| // considered a bug. For example: if the auth token is ovewritten, we can no longer upload |
| // to an invocation. If an entity name is ovewritten, we can no longer associate children |
| // with that entity. |
| // |
| // Please do not add information to this interface unless it is necessary to support |
| // creating Invocations in a distributed manner. |
| type LoggerState interface { |
| // SetEnvironment stores the ResultStore environment. |
| SetEnvironment(resultstore.Environment) bool |
| |
| // Environment is the ResultStore environment. |
| Environment() resultstore.Environment |
| |
| // SetAuthToken stores the Invocation's auth token. |
| SetAuthToken(string) bool |
| |
| // AuthToken is the Invocation's auth token. |
| AuthToken() string |
| |
| // SetInvocationID Stores the name of the Invocation. |
| SetInvocationID(id string) bool |
| |
| // SetInvocationName Stores the name of the Invocation. |
| SetInvocationName(name string) bool |
| |
| // InvocationID is the Invocation's ID. |
| InvocationID() string |
| |
| // InvocationName is the Invocation's name. |
| InvocationName() string |
| |
| // SetTarget stores a Target's name under its ID. |
| SetTarget(id, name string) bool |
| |
| // Target returns a Target's name, given its ID. |
| Target(id string) string |
| |
| // SetConfiguredTarget stores a ConfiguredTarget's name under its parent Target and |
| // Configuration IDs. |
| SetConfiguredTarget(targetID, configID string, name string) bool |
| |
| // ConfiguredTarget returns a ConfiguredTarget's name, given its parent Target and |
| // Configuration IDs. |
| ConfiguredTarget(targetID, configID string) string |
| |
| // SetTestAction stores a TestAction's name under its ancestor Target and Configuration |
| // IDs. |
| SetTestAction(targetID, configID, name string) bool |
| |
| // TestAction returns a TestAction's name, given its ancestor Target and Configuration |
| // IDs. |
| TestAction(targetID, configID string) string |
| } |
| |
| // NewLoggerState returns a new LoggerState. |
| func NewLoggerState() LoggerState { |
| return &loggerState{} |
| } |
| |
| // The default LoggerState implementation. |
| type loggerState struct { |
| EnvironmentValue resultstore.Environment `json:"environment"` |
| AuthTokenValue string `json:"auth_token"` |
| InvocationNameValue string `json:"invocation_name"` |
| InvocationIDValue string `json:"invocation_id"` |
| TargetNames map[string]string `json:"targets"` |
| ConfiguredTargetNames map[string]string `json:"configured_targets"` |
| TestActionNames map[string]string `json:"test_actions"` |
| } |
| |
| func (ls *loggerState) SetEnvironment(value resultstore.Environment) bool { |
| if ls.EnvironmentValue != "" && ls.EnvironmentValue != value { |
| return false |
| } |
| ls.EnvironmentValue = value |
| return true |
| } |
| |
| func (ls *loggerState) Environment() resultstore.Environment { |
| return ls.EnvironmentValue |
| } |
| |
| func (ls *loggerState) SetAuthToken(value string) bool { |
| if ls.AuthTokenValue != "" && ls.AuthTokenValue != value { |
| return false |
| } |
| ls.AuthTokenValue = value |
| return true |
| } |
| |
| func (ls *loggerState) AuthToken() string { |
| return ls.AuthTokenValue |
| } |
| |
| func (ls *loggerState) SetInvocationName(name string) bool { |
| if ls.InvocationNameValue != "" && ls.InvocationNameValue != name { |
| return false |
| } |
| ls.InvocationNameValue = name |
| return true |
| } |
| |
| func (ls *loggerState) InvocationName() string { |
| return ls.InvocationNameValue |
| } |
| |
| func (ls *loggerState) SetInvocationID(name string) bool { |
| if ls.InvocationIDValue != "" && ls.InvocationIDValue != name { |
| return false |
| } |
| ls.InvocationIDValue = name |
| return true |
| } |
| |
| func (ls *loggerState) InvocationID() string { |
| return ls.InvocationIDValue |
| } |
| |
| func (ls *loggerState) SetTarget(id, name string) bool { |
| ls.ensureTargets() |
| oldName := ls.Target(id) |
| if oldName != "" && oldName != name { |
| return false |
| } |
| ls.TargetNames[id] = name |
| return true |
| } |
| |
| func (ls *loggerState) Target(id string) string { |
| ls.ensureTargets() |
| val, _ := ls.TargetNames[id] |
| return val |
| } |
| |
| func (ls *loggerState) SetConfiguredTarget(targetID, configID string, name string) bool { |
| ls.ensureConfiguredTargets() |
| oldName := ls.ConfiguredTarget(targetID, configID) |
| if oldName != "" && oldName != name { |
| return false |
| } |
| id := ls.combineTargetAndConfigIDs(targetID, configID) |
| ls.ConfiguredTargetNames[id] = name |
| return true |
| } |
| |
| func (ls *loggerState) ConfiguredTarget(targetID, configID string) string { |
| ls.ensureConfiguredTargets() |
| id := ls.combineTargetAndConfigIDs(targetID, configID) |
| val, _ := ls.ConfiguredTargetNames[id] |
| return val |
| } |
| |
| func (ls *loggerState) SetTestAction(targetID, configID, name string) bool { |
| ls.ensureTestActions() |
| oldName := ls.TestAction(targetID, configID) |
| if oldName != "" && oldName != name { |
| return false |
| } |
| id := ls.combineTargetAndConfigIDs(targetID, configID) |
| ls.TestActionNames[id] = name |
| return true |
| } |
| |
| func (ls *loggerState) TestAction(targetID, configID string) string { |
| ls.ensureTestActions() |
| id := ls.combineTargetAndConfigIDs(targetID, configID) |
| val, _ := ls.TestActionNames[id] |
| return val |
| } |
| |
| func (ls *loggerState) ensureTargets() { |
| if ls.TargetNames == nil { |
| ls.TargetNames = make(map[string]string) |
| } |
| } |
| func (ls *loggerState) ensureConfiguredTargets() { |
| if ls.ConfiguredTargetNames == nil { |
| ls.ConfiguredTargetNames = make(map[string]string) |
| } |
| } |
| |
| func (ls *loggerState) ensureTestActions() { |
| if ls.TestActionNames == nil { |
| ls.TestActionNames = make(map[string]string) |
| } |
| } |
| |
| func (c loggerState) combineTargetAndConfigIDs(targetID, configID string) string { |
| return fmt.Sprintf("(target=%s,config=%s)", targetID, configID) |
| } |