Use CustomForm to display services

This commit is contained in:
nemunaire 2020-10-12 11:51:16 +02:00
parent 6ee1838ea4
commit 53da0f60cb
7 changed files with 214 additions and 22 deletions

133
api/service_settings.go Normal file
View File

@ -0,0 +1,133 @@
// Copyright or © or Copr. happyDNS (2020)
//
// contact@happydns.org
//
// This software is a computer program whose purpose is to provide a modern
// interface to interact with DNS systems.
//
// This software is governed by the CeCILL license under French law and abiding
// by the rules of distribution of free software. You can use, modify and/or
// redistribute the software under the terms of the CeCILL license as
// circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy, modify
// and redistribute granted by the license, users are provided only with a
// limited warranty and the software's author, the holder of the economic
// rights, and the successive licensors have only limited liability.
//
// In this respect, the user's attention is drawn to the risks associated with
// loading, using, modifying and/or developing or reproducing the software by
// the user in light of its specific status of free software, that may mean
// that it is complicated to manipulate, and that also therefore means that it
// is reserved for developers and experienced professionals having in-depth
// computer knowledge. Users are therefore encouraged to load and test the
// software's suitability as regards their requirements in conditions enabling
// the security of their systems and/or data to be ensured and, more generally,
// to use and operate it in the same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
package api
import (
"encoding/json"
"io"
"git.happydns.org/happydns/config"
"git.happydns.org/happydns/forms"
"git.happydns.org/happydns/model"
"git.happydns.org/happydns/services"
"git.happydns.org/happydns/storage"
)
func init() {
router.POST("/api/domains/:domain/zone/:zoneid/:subdomain/services/*psid", apiAuthHandler(domainHandler(zoneHandler(getServiceSettingsState))))
}
type ServiceSettingsState struct {
FormState
happydns.Service
}
type ServiceSettingsResponse struct {
FormResponse
Services map[string][]*happydns.ServiceCombined `json:"services,omitempty"`
}
func getServiceSettingsState(cfg *config.Options, req *RequestResources, body io.Reader) Response {
psid := string(req.Ps.ByName("psid"))
// Remove the leading slash
if len(psid) > 1 {
psid = psid[1:]
}
pvr, err := svcs.FindService(psid)
if err != nil {
return APIErrorResponse{
err: err,
}
}
var ups ServiceSettingsState
ups.Service = pvr
err = json.NewDecoder(body).Decode(&ups)
if err != nil {
return APIErrorResponse{
err: err,
}
}
form, err := formDoState(cfg, req, &ups.FormState, ups.Service, forms.GenDefaultSettingsForm)
if err != nil {
if err != forms.DoneForm {
return APIErrorResponse{
err: err,
}
} else if ups.Id == 0 {
// Append a new Service
if err = req.Zone.AppendService(string(req.Ps.ByName("subdomain")), req.Domain.DomainName, &happydns.ServiceCombined{Service: ups.Service}); err != nil {
return APIErrorResponse{
err: err,
}
} else if err = storage.MainStore.UpdateZone(req.Zone); err != nil {
return APIErrorResponse{
err: err,
}
} else {
return APIResponse{
response: ServiceSettingsResponse{
Services: req.Zone.Services,
FormResponse: FormResponse{Redirect: ups.Redirect},
},
}
}
} else {
// Update an existing Service
if err = req.Zone.EraseServiceWithoutMeta(string(req.Ps.ByName("subdomain")), req.Domain.DomainName, ups.IdB, ups); err != nil {
return APIErrorResponse{
err: err,
}
} else if err = storage.MainStore.UpdateZone(req.Zone); err != nil {
return APIErrorResponse{
err: err,
}
} else {
return APIResponse{
response: ServiceSettingsResponse{
Services: req.Zone.Services,
FormResponse: FormResponse{Redirect: ups.Redirect},
},
}
}
}
}
return APIResponse{
response: SourceSettingsResponse{
FormResponse: FormResponse{From: form},
},
}
}

View File

@ -42,10 +42,23 @@
<slot />
<h-resource-value
v-if="type"
ref="resource"
edit
:services="services"
:specs="{}"
:type="type"
:value="val"
@input="val = $event;$emit('input', val)"
/>
<h-fields
v-if="form.fields && val"
v-else-if="form.fields && val"
ref="resource"
edit
:fields="form.fields"
:services="services"
:value="val"
@input="val = $event;$emit('input', val)"
/>
@ -61,7 +74,8 @@ export default {
name: 'HCustomForm',
components: {
hFields: () => import('@/components/hFields')
hFields: () => import('@/components/hFields'),
hResourceValue: () => import('@/components/hResourceValue')
},
props: {
@ -69,6 +83,14 @@ export default {
type: Object,
required: true
},
services: {
type: Object,
default: () => {}
},
type: {
type: String,
default: ''
},
value: {
type: Object,
required: true
@ -94,6 +116,10 @@ export default {
},
methods: {
saveChildrenValues () {
this.$refs.resource.saveChildrenValues()
},
updateValues () {
if (this.value != null) {
this.val = Object.assign({}, this.value)

View File

@ -36,11 +36,13 @@
<h-resource-value
v-for="(specs, index) in fields"
v-show="edit || !specs.secret"
ref="child"
:key="index"
:edit="edit"
:index="index"
:services="services"
:specs="specs"
type="string"
:type="specs.type"
:value="val[specs.id]"
@input="val[specs.id] = $event;$emit('input', val)"
/>
@ -64,6 +66,10 @@ export default {
type: Array,
required: true
},
services: {
type: Object,
default: () => {}
},
value: {
type: Object,
required: true
@ -105,6 +111,12 @@ export default {
}
},
saveChildrenValues () {
this.$refs.child.forEach(function (row) {
row.saveChildrenValues()
}, this)
},
updateValues () {
if (this.value != null) {
this.val = Object.assign({}, this.value)

View File

@ -90,16 +90,16 @@
</b-tab>
</b-tabs>
<div v-else-if="step === 2">
<p>
Fill the information for the {{ services[svcSelected].name }} at <span class="text-monospace">{{ dn | fqdn(domain.domain) }}</span>:
</p>
<h-resource-value ref="addModalResources" v-model="svcData.Service" edit :services="services" :type="svcSelected" />
<h-custom-form v-if="form" ref="addModalResources" v-model="svcData.Service" :form="form" :services="services" :type="svcSelected" />
<b-spinner v-else label="Spinning" />
</div>
</form>
</b-modal>
</template>
<script>
import CustomForm from '@/mixins/customForm'
import ServicesApi from '@/services/ServicesApi'
import SourcesApi from '@/services/SourcesApi'
import ValidateDomain from '@/mixins/validateDomain'
import ZoneApi from '@/services/ZoneApi'
@ -108,10 +108,10 @@ export default {
name: 'HModalAddService',
components: {
hResourceValue: () => import('@/components/hResourceValue')
hCustomForm: () => import('@/components/hCustomForm')
},
mixins: [ValidateDomain],
mixins: [CustomForm, ValidateDomain],
props: {
domain: {
@ -311,6 +311,10 @@ export default {
})
},
getFormSettings (state, settings, recallid) {
return ServicesApi.getFormSettings(this.domain.domain, this.zoneId, this.dn, this.svcSelected, state, settings, recallid)
},
handleModalSvcOk (bvModalEvt) {
bvModalEvt.preventDefault()
@ -321,6 +325,8 @@ export default {
} else if (this.step === 1 && this.svcSelected !== null) {
this.step = 2
this.svcData = { Service: {}, _svctype: this.svcSelected }
this.resetSettings()
this.updateSettingsForm()
} else if (this.step === 2 && this.svcSelected !== null) {
this.$refs.addModalResources.saveChildrenValues()
@ -371,6 +377,7 @@ export default {
this.svcSelected = data._svctype
this.svcData = data
this.update = true
this.updateSettingsForm()
} else {
this.svcSelected = null
this.svcData = { Service: {} }

View File

@ -40,18 +40,6 @@ export default {
}
},
computed: {
isLoading () {
return this.form == null || this.sourceSpecs == null
}
},
mounted () {
console.log('mounted customform')
this.resetSettings()
this.updateSettingsForm()
},
methods: {
loadState (toState, recallid, cbSuccess, cbFail) {
this.getFormSettings(toState, this.settings, recallid)

View File

@ -35,6 +35,17 @@ import SourceSettingsApi from '@/services/SourceSettingsApi'
export default {
mixins: [CustomForm],
computed: {
isLoading () {
return this.form == null || this.sourceSpecs == null
}
},
mounted () {
this.resetSettings()
this.updateSettingsForm()
},
methods: {
getFormSettings (state, settings, recallid) {
return SourceSettingsApi.getSourceSettings(this.sourceSpecsSelected, state, settings, recallid)

View File

@ -35,8 +35,23 @@ export default {
getServices (domain) {
return Api().get('/api/service_specs')
},
getFormSettings (domain, id, subdomain, service, state, settings, recallid) {
if (!state) {
state = 0
}
if (!settings) {
settings = {}
}
settings.state = state
if (recallid) {
settings.recall = parseInt(recallid)
}
return Api().post('/api/domains/' + encodeURIComponent(domain) + '/zone/' + encodeURIComponent(id) + '/' + encodeURIComponent(subdomain) + '/services/' + encodeURIComponent(service), settings)
},
updateService (service) {
var serviceType = serviceType[0] === '*' ? serviceType.substr(1) : serviceType
var serviceType = service[0] === '*' ? service.substr(1) : service
return Api().get('/api/service_specs/' + serviceType)
}
}