diff --git a/api/cluster.go b/api/cluster.go deleted file mode 100644 index a6403345..00000000 --- a/api/cluster.go +++ /dev/null @@ -1,51 +0,0 @@ -package api - -import "encoding/json" - -// ClusterSummary contains a summary of the most recent status of a cluster. -type ClusterSummary struct { - Cluster string `json:"cluster"` - LatestReportSet *ReportSet `json:"latestReportSet"` -} - -// ReportSet groups one or more reports of different packages with the same timestamp for the same cluster. -type ReportSet struct { - Cluster string `json:"-"` - Timestamp Time `json:"timestamp"` - FailureCount int `json:"failureCount"` - SuccessCount int `json:"successCount"` - Reports []*ReportSummary `json:"reports"` -} - -// ReportSummary constains a summary of a report. -type ReportSummary struct { - ID string `json:"id"` - Package string `json:"package"` - Cluster string `json:"-"` - Timestamp Time `json:"-"` - FailureCount int `json:"failureCount"` - SuccessCount int `json:"successCount"` -} - -// UnmarshalJSON unmarshals a ClusterSummary. -func (c *ClusterSummary) UnmarshalJSON(data []byte) error { - type Alias ClusterSummary - aux := &struct { - *Alias - }{ - Alias: (*Alias)(c), - } - - if err := json.Unmarshal(data, &aux); err != nil { - return err - } - - c.LatestReportSet.Cluster = c.Cluster - - for idx := range c.LatestReportSet.Reports { - c.LatestReportSet.Reports[idx].Cluster = c.LatestReportSet.Cluster - c.LatestReportSet.Reports[idx].Timestamp = c.LatestReportSet.Timestamp - } - - return nil -} diff --git a/api/cluster_test.go b/api/cluster_test.go deleted file mode 100644 index 30eca441..00000000 --- a/api/cluster_test.go +++ /dev/null @@ -1,75 +0,0 @@ -package api - -import ( - "encoding/json" - "reflect" - "testing" - "time" -) - -func TestClusterSummaryUnmarshalJSON(t *testing.T) { - data := `{ - "cluster": "exampleCluster", - "latestReportSet": { - "timestamp": "2015-10-21T07:28:42Z", - "failureCount": 4, - "successCount": 1, - "reports": [ - { - "id": "exampleReport1", - "package": "examplePackage.ID.1", - "failureCount": 2, - "successCount": 1 - }, - { - "id": "exampleReport2", - "package": "examplePackage.ID.2", - "failureCount": 2, - "successCount": 0 - } - ] - } -}` - ts, err := time.Parse(TimeFormat, "2015-10-21T07:28:42Z") - if err != nil { - t.Fatalf("%+v", err) - } - - want := ClusterSummary{ - Cluster: "exampleCluster", - LatestReportSet: &ReportSet{ - Cluster: "exampleCluster", - Timestamp: Time{Time: ts}, - FailureCount: 4, - SuccessCount: 1, - Reports: []*ReportSummary{ - { - ID: "exampleReport1", - Package: "examplePackage.ID.1", - Cluster: "exampleCluster", - Timestamp: Time{Time: ts}, - FailureCount: 2, - SuccessCount: 1, - }, - { - ID: "exampleReport2", - Package: "examplePackage.ID.2", - Cluster: "exampleCluster", - Timestamp: Time{Time: ts}, - FailureCount: 2, - SuccessCount: 0, - }, - }, - }, - } - - var got ClusterSummary - err = json.Unmarshal([]byte(data), &got) - if err != nil { - t.Fatalf("unexpected error: %+v", err) - } - - if !reflect.DeepEqual(got, want) { - t.Fatalf("got != want: got=%+v, want=%+v", got, want) - } -} diff --git a/api/report.go b/api/report.go deleted file mode 100644 index 15cba734..00000000 --- a/api/report.go +++ /dev/null @@ -1,122 +0,0 @@ -package api - -// Report contains the fields of a Preflight report -type Report struct { - // Unique ID of the report. - ID string `json:"id"` - // PreflightVersion indicates the version of preflight this report was generated with. - PreflightVersion string `json:"preflight-version"` - // Timestamp indicates when the report was generated. - Timestamp Time `json:"timestamp"` - // Cluster indicates which was the target of the report. - Cluster string `json:"cluster"` - // Package indicates which package was used for the report. (deprecated) - Package string `json:"package"` - // PackageInformation contains all the information about the package that was used to generate the report. - PackageInformation PackageInformation `json:"package-information"` - // Name is the name of the package that was used for this report. - Name string `json:"name"` - // Description is the description of the package that was used for this report. - Description string `json:"description,omitempty"` - // Sections contains the sections of the package that was used for this report. - Sections []ReportSection `json:"sections,omitempty"` -} - -// Summarize produces as ReportSummary from a Report -func (r *Report) Summarize() ReportSummary { - var successes, failures int - for _, section := range r.Sections { - for _, rule := range section.Rules { - if rule.Success { - successes++ - } else { - failures++ - } - } - } - - return ReportSummary{ - ID: r.ID, - Package: r.Package, - Cluster: r.Cluster, - Timestamp: r.Timestamp, - FailureCount: failures, - SuccessCount: successes, - } -} - -// GetReportMetadata returns the ReportMetadata for the Report. -func (r *Report) GetReportMetadata() *ReportMetadata { - return &ReportMetadata{ - ID: r.ID, - Timestamp: r.Timestamp, - Cluster: r.Cluster, - Package: r.Package, - PackageInformation: r.PackageInformation, - PreflightVersion: r.PreflightVersion, - } -} - -// PackageInformation contains all the details to identify a package. -type PackageInformation struct { - // Namespace the package belongs to. - Namespace string `json:"namespace"` - // ID is the ID of the package. - ID string `json:"id"` - // Version is the version of the package. - Version string `json:"version"` - // SchemaVersion is the version of the Preflight package schema. - SchemaVersion string `json:"schema-version"` -} - -// ReportSection contains the fields of a section inside a Report -type ReportSection struct { - // ID is the ID of the section. - ID string `json:"id"` - // Name is the name of the section. - Name string `json:"name"` - // Description is the description of the section. - Description string `json:"description,omitempty"` - // Rules contain all the rules in the section. - Rules []ReportRule `json:"rules,omitempty"` -} - -// ReportRule contains the fields of a rule inside a Report -type ReportRule struct { - // ID is the id of the rule. - ID string `json:"id"` - // Name is a shortname for the rule. - Name string `json:"name"` - // Description is a text describing what the rule is about. - Description string `json:"description,omitempty"` - // Manual indicated whether the rule can be evaluated automatically by Preflight or requires manual intervention. - Manual bool `json:"manual,omitempty"` - // Remediation is a text describing how to fix a failure of the rule. - Remediation string `json:"remediation,omitempty"` - // Links contains useful links related to the rule. - Links []string `json:"links,omitempty"` - // Success indicates whether the check was a success or not. - Success bool `json:"success"` - // Value contains the raw result of the check. - Value interface{} `json:"value,omitempty"` - // Violations contains the list of messages from the execution of the check - Violations interface{} `json:"violations"` - // Missing indicates whether the Rego rule was missing or not. - Missing bool `json:"missing"` -} - -// ReportMetadata contains metadata about a report -type ReportMetadata struct { - // Unique ID of the report. - ID string `json:"id"` - // Timestamp indicates when the report was generated. - Timestamp Time `json:"timestamp"` - // Cluster indicates which was the target of the report. - Cluster string `json:"cluster"` - // Deprecated: Package indicates which package was used for the report. - Package string `json:"package"` - // PackageInformation contains all the information about the package that was used to generate the report. - PackageInformation PackageInformation `json:"package-information"` - // PreflightVersion indicates the version of preflight this report was generated with. - PreflightVersion string `json:"preflight-version"` -}